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);
}

  

最新文章

  1. 15天玩转redis —— 第九篇 发布/订阅模式
  2. 【原】为什么选择iPhone5的分辨率作为H5视觉稿尺寸
  3. .net配置文件读取
  4. 在这个变化的年代,IT人的方向在哪里?看两个故事
  5. Working with HTTP
  6. eclipse运行maven的jetty插件内存溢出
  7. Map,HashMap
  8. socket异步编程--libevent的使用
  9. MYSQL日常操作命令再熟悉
  10. Java程序员容易犯的10个错误
  11. QT程序启动界面的使用
  12. mysql入库中文乱码问题
  13. MVC源码分析 - 路由匹配
  14. Codeforces#371 Div2
  15. Linux格式化、挂载及卸载数据盘
  16. linux系统之tr命令
  17. linux 终端 pac ssh登录工具使用教程
  18. 机器学习笔记 1 LMS和梯度下降(批梯度下降) 20170617
  19. IntelliJ IDEA 使用经验总结
  20. 生成CFree 5.0 注册码

热门文章

  1. pytest_用例运行级别_模块级
  2. meta标签 使用说明(http-equiv、refresh、seo)
  3. 关于C++中nothrow的某某某
  4. Microsoft Office Excel
  5. 斯坦福【概率与统计】课程笔记(六):EDA | 标准差和方差
  6. 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
  7. Python做个小游戏
  8. f-strings格式化输出
  9. Python面试题之如何用Python来发送邮件?
  10. matplotlib系列——折线图