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

  

Gson extend 思路的更多相关文章

  1. vue2入坑随记(二) -- 自定义动态组件

    学习了Vue全家桶和一些UI基本够用了,但是用元素的方式使用组件还是不够灵活,比如我们需要通过js代码直接调用组件,而不是每次在页面上通过属性去控制组件的表现.下面讲一下如何定义动态组件. Vue.e ...

  2. Vue 插件写法

    都说Vue2简单,上手容易,但小马过河,自己试了才晓得,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前随便拿全局变量和修改dom的锤子不能用了,变换到关注数据本身.vue的 ...

  3. h5 录音 自动生成proto Js语句 UglifyJS-- 对你的js做了什么 【原码笔记】-- protobuf.js 与 Long.js 【微信开发】-- 发送模板消息 能编程与会编程 vue2入坑随记(二) -- 自定义动态组件 微信上传图片

    得益于前辈的分享,做了一个h5录音的demo.效果图如下: 点击开始录音会先弹出确认框: 首次确认允许后,再次录音不需要再确认,但如果用户点击禁止,则无法录音: 点击发送 将录音内容发送到对话框中.点 ...

  4. 图片放大功能插件及jquery.extend函数理解

    前端时间,产品提出社区评论中的图片需要有放大功能.感觉可以共用,所以就想整合一个插件,过程中也借鉴了一些例子. 分析下自己的代码思路: var scaleImg = function(opts) { ...

  5. 点击更多button显示更多数据的功能实现思路代码

    此功能是根据自己思路去慢慢做出来的,做的不够专业,希望有懂这个的前辈给自己指点指点. //分界线———————————————————————————————————————————————————— ...

  6. Gson解析数组多类型元素

    本文的出现是为了解决以下需求:使用Gson对json数组进行解析,但是数组里面元素的类型是多种的.数据如下: {"list":[{ "type":0, &quo ...

  7. Android网络之数据解析----使用Google Gson解析Json数据

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. GSON

    { "data": [ { "children": [ { "id": 10007, "title": "北京 ...

  9. 把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进)

    #把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进) ##背景 项目中经常需要把JSON数据填充到页面表单,一开始我使用easyui自带的form load方法,觉得效率很低,经 ...

随机推荐

  1. web前端典型示例

    1.轨迹回放:https://openlayers.org/en/v4.6.5/examples/feature-move-animation.html https://blog.csdn.net/s ...

  2. DTED文件结构

    注:DTED层级为1时,每列总计2414字节,包含1201个高度信息:DTED层级为2时,每列总计7214字节,包含3601个高度信息:DTED层级为3时,每列包含9001个高度信息. 每列数据前八个 ...

  3. JVM调优(三)——基于Btrace的监控调试

    JVM调优(三)--基于Btrace的监控调试 简介 Btrace可以动态地向目标应用程序的字节码注入追踪代码 用到的技术: JavaComplierApi.JVMTI.Agent.Instrumen ...

  4. delphi vlc 安装bug 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"

    处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" [摘要:http://blog.csdn ...

  5. Centos7从零开始】Centos 下硬盘分区的最佳方案

    https://www.cnblogs.com/chen1970/p/11071590.html 在对硬盘进行分区前,应该先弄清楚计算机担负的工作及硬盘的容量有多大,还要考虑到以下几个问题: 第一点也 ...

  6. js 监听input 实现数据绑定

    <!DOCTYPE html> <html> <head> <script> function checkField(val) { //alert(&q ...

  7. 搭建 webpack、react 开发环境(三)

    配置 react-router-dom   我们开发一个 React 工程肯定不是一两个“页面”就可以满足需求的,所以我们需要一个在多个“页面”中跳转的功能,在使用 React 构建的单页面应用中,要 ...

  8. js 中的深拷贝与浅拷贝

    在面试中经常会问到js的深拷贝和浅拷贝,也常常让我们手写,下面我们彻底搞懂js的深拷贝与浅拷贝. 在js中 Array 和 Object  这种引用类型的值,当把一个变量赋值给另一个变量时,这个值得副 ...

  9. go读写文本文件

    一.文件读取 1. 将整个文件读取到内存中 package main import ( "flag" "fmt" "io/ioutil" ) ...

  10. 【记录】Mysql 建表注意事项

    博主最近打算搭建商城,由于之前对建表只有很浅显的理解,没有太过深入了解,建表过程中遇到一些问题,现记录如下, 如有问题请各位留言指正,感激不尽: 建表时设置如何设置联合主键?如下标红处: CREATE ...