1. package org.rx.core.internal;
  2.  
  3. import com.google.gson.*;
  4. import net.sf.cglib.proxy.Enhancer;
  5. import net.sf.cglib.proxy.MethodInterceptor;
  6. import net.sf.cglib.proxy.MethodProxy;
  7. import org.rx.beans.JsonObject;
  8. import org.rx.core.Reflects;
  9.  
  10. import java.lang.reflect.Method;
  11. import java.math.BigDecimal;
  12. import java.math.BigInteger;
  13.  
  14. import static org.rx.core.Contract.tryAs;
  15.  
  16. public class GsonWrapper {
  17. public static JsonObject wrap(com.google.gson.JsonObject obj) {
  18. return (JsonObject) Enhancer.create(JsonObject.class, new MethodInterceptor() {
  19. Gson gson = new Gson();
  20.  
  21. @Override
  22. public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  23. switch (method.getName()) {
  24. case "deepCopy":
  25. return wrap(obj.deepCopy());
  26. case "getJsonObject": {
  27. String property = (String) args[0];
  28. return wrap(obj.getAsJsonObject(property));
  29. }
  30. case "add": {
  31. String property = (String) args[0];
  32. Object value = args[1];
  33. if (!tryAs(value, String.class, p -> obj.addProperty(property, p))
  34. && !tryAs(value, Number.class, p -> obj.addProperty(property, p))
  35. && !tryAs(value, Boolean.class, p -> obj.addProperty(property, p))) {
  36. obj.add(property, gson.toJsonTree(value));
  37. }
  38. return null;
  39. }
  40. case "remove":
  41. String property = (String) args[0];
  42. obj.remove(property);
  43. return null;
  44. }
  45. String fg = "get";
  46. if (method.getName().startsWith("is")) {
  47. String property = (String) args[0];
  48. JsonElement element = obj.get(property);
  49. return Reflects.invokeMethod(element.getClass(), element, method.getName());
  50. } else if (method.getName().startsWith(fg)) {
  51. String property = (String) args[0];
  52. JsonElement element = obj.get(property);
  53. return Reflects.invokeMethod(element.getClass(), element, "getAs" + method.getName().substring(fg.length()));
  54. }
  55. return methodProxy.invoke(obj, args);
  56. }
  57. });
  58. }
  59. }

  

  1. package org.rx.beans;
  2.  
  3. import com.google.gson.JsonArray;
  4. import com.google.gson.JsonPrimitive;
  5.  
  6. import java.math.BigDecimal;
  7. import java.math.BigInteger;
  8.  
  9. public interface JsonObject {
  10. JsonObject deepCopy();
  11.  
  12. int size();
  13.  
  14. boolean has(String property);
  15.  
  16. void add(String property, Object value);
  17.  
  18. void remove(String property);
  19.  
  20. boolean isJsonArray(String property);
  21.  
  22. boolean isJsonObject(String property);
  23.  
  24. boolean isJsonPrimitive(String property);
  25.  
  26. boolean isJsonNull(String property);
  27.  
  28. JsonObject getJsonObject(String property);
  29.  
  30. JsonArray getJsonArray(String property);
  31.  
  32. JsonPrimitive getJsonPrimitive(String property);
  33.  
  34. boolean getBoolean(String property);
  35.  
  36. Number getNumber(String property);
  37.  
  38. String getString(String property);
  39.  
  40. double getDouble(String property);
  41.  
  42. float getFloat(String property);
  43.  
  44. long getLong(String property);
  45.  
  46. int getInt(String property);
  47.  
  48. byte getByte(String property);
  49.  
  50. BigDecimal getBigDecimal(String property);
  51.  
  52. BigInteger getBigInteger(String property);
  53.  
  54. short getShort(String property);
  55. }

  

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. Linux测试端口的连通性的四种方法

    目录 1.telnet 2.ssh 3.crul 4.wget 方法一.telnet telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口的连通性,具体用法 ...

  2. python中将'12345'转换为12345,不要使用int

    #将'12345'转换为12345,不要使用int s = " #定义一个数字变量 ret = 0 for x in s : ret = ret*10 +( ord(x)-ord(" ...

  3. springBoot JPA PageAble分页查询出错,PropertyReferenceException: No property creation found for type

    PropertyReferenceException: No property creation found for type @RequestParam(required = false, defa ...

  4. centos清理缓存

    释放网页缓存(To free pagecache): echo 1 > /proc/sys/vm/drop_caches 释放目录项和索引(To free dentries and inodes ...

  5. HBuilder的一些常用快捷键

    Alt + [ 匹配括号 Alt + ↓跳转到下一个可编辑区Ctrl + Alt + j 合并下一行Ctrl + Alt + ←选择助手Ctrl + 回车 换行Ctrl + Shift + 回车 向上 ...

  6. UIsearchBar 自定义样式

    对于修改输入框圆角 的需求, 可以通过修改搜索框背景图片的方式设置. - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgro ...

  7. 基于windows下,node.js之npm

    1.下载node.js一路安装下去 在开始 node文件夹下,打开cmd 2.创建一个开发目录 mkdir reactQa && cd reactQa 3.初始化一个nmp的开发环境 ...

  8. boostrap中lg,md,sm,xs分别对应的像素宽度

    col-xs-   超小屏幕 手机 (<768px)col-sm-  小屏幕 平板 (≥768px)col-md-  中等屏幕 桌面显示器 (≥992px)col-lg-    大屏幕 大桌面显 ...

  9. leetcode.排序.347前k个高频元素-Java

    1. 具体题目 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums ...

  10. 51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)

    Level:   Medium 题目描述: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes ...