Gson extend 思路
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 思路的更多相关文章
- vue2入坑随记(二) -- 自定义动态组件
学习了Vue全家桶和一些UI基本够用了,但是用元素的方式使用组件还是不够灵活,比如我们需要通过js代码直接调用组件,而不是每次在页面上通过属性去控制组件的表现.下面讲一下如何定义动态组件. Vue.e ...
- Vue 插件写法
都说Vue2简单,上手容易,但小马过河,自己试了才晓得,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前随便拿全局变量和修改dom的锤子不能用了,变换到关注数据本身.vue的 ...
- h5 录音 自动生成proto Js语句 UglifyJS-- 对你的js做了什么 【原码笔记】-- protobuf.js 与 Long.js 【微信开发】-- 发送模板消息 能编程与会编程 vue2入坑随记(二) -- 自定义动态组件 微信上传图片
得益于前辈的分享,做了一个h5录音的demo.效果图如下: 点击开始录音会先弹出确认框: 首次确认允许后,再次录音不需要再确认,但如果用户点击禁止,则无法录音: 点击发送 将录音内容发送到对话框中.点 ...
- 图片放大功能插件及jquery.extend函数理解
前端时间,产品提出社区评论中的图片需要有放大功能.感觉可以共用,所以就想整合一个插件,过程中也借鉴了一些例子. 分析下自己的代码思路: var scaleImg = function(opts) { ...
- 点击更多button显示更多数据的功能实现思路代码
此功能是根据自己思路去慢慢做出来的,做的不够专业,希望有懂这个的前辈给自己指点指点. //分界线———————————————————————————————————————————————————— ...
- Gson解析数组多类型元素
本文的出现是为了解决以下需求:使用Gson对json数组进行解析,但是数组里面元素的类型是多种的.数据如下: {"list":[{ "type":0, &quo ...
- Android网络之数据解析----使用Google Gson解析Json数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- GSON
{ "data": [ { "children": [ { "id": 10007, "title": "北京 ...
- 把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进)
#把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进) ##背景 项目中经常需要把JSON数据填充到页面表单,一开始我使用easyui自带的form load方法,觉得效率很低,经 ...
随机推荐
- SQL各种JOIN
JOIN(= INNER JOIN):返回匹配的结果,没有匹配则没结果: LEFT JOIN(= LEFT OUTER JOIN):返回匹配的与左表的所有数据: RIGHT JOIN(= RIGHT ...
- 转载:Linux下启动和关闭Weblogic(管理服务器+被管服务器)
转载自:http://www.cnblogs.com/nick-huang/p/3834134.html 感谢! Weblogic的管理服务器和被管服务器的启动.关闭,偶尔会用到,却又不常用,导致需 ...
- MDX members使用
Members (Set) 函数返回该指定层次结构内所有成员(不包括计算成员)的集: Members (String) 函数返回已指定名称的单个成员. 通常,将 Members (String) 函数 ...
- response.setHeader();小结
response.setHeader():1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但与被传输的实体内容没有关系的信息头,如Data,Pragma 主要: Cac ...
- JS中获取元素使用getElementByID()、getElementsByName()、getElementsByTagName()的用法和区别
Web标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问Documnent中的任一个标签: 1 g ...
- 关于Java中编码集的有趣现象和解释
这是在整理另一篇博客的时候发现的一个有趣的现象,是这样描述的:我们都知道Java默认使用的是UniCode编码集,我们也知道char类型占用两个字节.所以奇怪的现象又发生了(见代码): @Test p ...
- SQL 批量插入数据
后面进行完善修改. /*批量插入数据*/ 这个比较完善.直接插入数据库表. INSERT INTO `goods_transverter` ( `code`,`es_id`,`barcode`, `n ...
- Mycat+Pxc的配置
1 schema.xml配置文件 Balance属性 负载均称类型 0:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上 1:全部的readHost与stand by writeH ...
- 关于列表倒序输出的几种方法——python第7天
项目:将列表li1 = [1, 6, 4, 3, 7, 9]倒序输出为[9, 7, 6, 4, 3, 1] li2 = ['a', 'm', 's', 'g']倒序输出为['g', 's', 'm', ...
- linux缺頁異常處理--內核空間[v3.10]
缺頁異常被觸發通常有兩種情況—— 1.程序設計的不當導致訪問了非法的地址 2.訪問的地址是合法的,但是該地址還未分配物理頁框 下面解釋一下第二種情況,這是虛擬內存管理的一個特性.盡管每個進程獨立擁有3 ...