Gson extend 思路
2024-10-07 17:01:34
package org.rx.core.internal; import com.google.gson.*;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.rx.beans.JsonObject;
import org.rx.core.Reflects; import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger; import static org.rx.core.Contract.tryAs; public class GsonWrapper {
public static JsonObject wrap(com.google.gson.JsonObject obj) {
return (JsonObject) Enhancer.create(JsonObject.class, new MethodInterceptor() {
Gson gson = new Gson(); @Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
switch (method.getName()) {
case "deepCopy":
return wrap(obj.deepCopy());
case "getJsonObject": {
String property = (String) args[0];
return wrap(obj.getAsJsonObject(property));
}
case "add": {
String property = (String) args[0];
Object value = args[1];
if (!tryAs(value, String.class, p -> obj.addProperty(property, p))
&& !tryAs(value, Number.class, p -> obj.addProperty(property, p))
&& !tryAs(value, Boolean.class, p -> obj.addProperty(property, p))) {
obj.add(property, gson.toJsonTree(value));
}
return null;
}
case "remove":
String property = (String) args[0];
obj.remove(property);
return null;
}
String fg = "get";
if (method.getName().startsWith("is")) {
String property = (String) args[0];
JsonElement element = obj.get(property);
return Reflects.invokeMethod(element.getClass(), element, method.getName());
} else if (method.getName().startsWith(fg)) {
String property = (String) args[0];
JsonElement element = obj.get(property);
return Reflects.invokeMethod(element.getClass(), element, "getAs" + method.getName().substring(fg.length()));
}
return methodProxy.invoke(obj, args);
}
});
}
}
package org.rx.beans; import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive; import java.math.BigDecimal;
import java.math.BigInteger; public interface JsonObject {
JsonObject deepCopy(); int size(); boolean has(String property); void add(String property, Object value); void remove(String property); boolean isJsonArray(String property); boolean isJsonObject(String property); boolean isJsonPrimitive(String property); boolean isJsonNull(String property); JsonObject getJsonObject(String property); JsonArray getJsonArray(String property); JsonPrimitive getJsonPrimitive(String property); boolean getBoolean(String property); Number getNumber(String property); String getString(String property); double getDouble(String property); float getFloat(String property); long getLong(String property); int getInt(String property); byte getByte(String property); BigDecimal getBigDecimal(String property); BigInteger getBigInteger(String property); short getShort(String property);
}
最新文章
- 15天玩转redis —— 第九篇 发布/订阅模式
- 【原】为什么选择iPhone5的分辨率作为H5视觉稿尺寸
- .net配置文件读取
- 在这个变化的年代,IT人的方向在哪里?看两个故事
- Working with HTTP
- eclipse运行maven的jetty插件内存溢出
- Map,HashMap
- socket异步编程--libevent的使用
- MYSQL日常操作命令再熟悉
- Java程序员容易犯的10个错误
- QT程序启动界面的使用
- mysql入库中文乱码问题
- MVC源码分析 - 路由匹配
- Codeforces#371 Div2
- Linux格式化、挂载及卸载数据盘
- linux系统之tr命令
- linux 终端 pac ssh登录工具使用教程
- 机器学习笔记 1 LMS和梯度下降(批梯度下降) 20170617
- IntelliJ IDEA 使用经验总结
- 生成CFree 5.0 注册码