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方法,觉得效率很低,经 ...
随机推荐
- Linux测试端口的连通性的四种方法
目录 1.telnet 2.ssh 3.crul 4.wget 方法一.telnet telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口的连通性,具体用法 ...
- python中将'12345'转换为12345,不要使用int
#将'12345'转换为12345,不要使用int s = " #定义一个数字变量 ret = 0 for x in s : ret = ret*10 +( ord(x)-ord(" ...
- springBoot JPA PageAble分页查询出错,PropertyReferenceException: No property creation found for type
PropertyReferenceException: No property creation found for type @RequestParam(required = false, defa ...
- centos清理缓存
释放网页缓存(To free pagecache): echo 1 > /proc/sys/vm/drop_caches 释放目录项和索引(To free dentries and inodes ...
- HBuilder的一些常用快捷键
Alt + [ 匹配括号 Alt + ↓跳转到下一个可编辑区Ctrl + Alt + j 合并下一行Ctrl + Alt + ←选择助手Ctrl + 回车 换行Ctrl + Shift + 回车 向上 ...
- UIsearchBar 自定义样式
对于修改输入框圆角 的需求, 可以通过修改搜索框背景图片的方式设置. - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgro ...
- 基于windows下,node.js之npm
1.下载node.js一路安装下去 在开始 node文件夹下,打开cmd 2.创建一个开发目录 mkdir reactQa && cd reactQa 3.初始化一个nmp的开发环境 ...
- boostrap中lg,md,sm,xs分别对应的像素宽度
col-xs- 超小屏幕 手机 (<768px)col-sm- 小屏幕 平板 (≥768px)col-md- 中等屏幕 桌面显示器 (≥992px)col-lg- 大屏幕 大桌面显 ...
- leetcode.排序.347前k个高频元素-Java
1. 具体题目 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums ...
- 51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)
Level: Medium 题目描述: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes ...