PageRenderTime 381ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/resty-common/src/main/java/cn/dreampie/common/util/json/ModelDeserializer.java

https://gitlab.com/xialeizhou/resty
Java | 161 lines | 136 code | 18 blank | 7 comment | 29 complexity | b8b574e72560e2382fb046f131a049eb MD5 | raw file
  1. package cn.dreampie.common.util.json;
  2. import cn.dreampie.common.Entity;
  3. import cn.dreampie.common.util.Stringer;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONException;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.alibaba.fastjson.parser.DefaultJSONParser;
  8. import com.alibaba.fastjson.parser.JSONToken;
  9. import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
  10. import java.lang.reflect.Method;
  11. import java.lang.reflect.ParameterizedType;
  12. import java.lang.reflect.Type;
  13. import java.util.*;
  14. /**
  15. * Created by ice on 14-12-31.
  16. */
  17. public enum ModelDeserializer implements ObjectDeserializer {
  18. INSTANCE;
  19. public static ModelDeserializer instance() {
  20. return INSTANCE;
  21. }
  22. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  23. Map<String, Object> map = parser.parseObject();
  24. Class<?> clazz = (Class<?>) type;
  25. if (clazz.isInterface()) {
  26. throw new JSONException("Unsupport type " + type);
  27. }
  28. try {
  29. Entity<T> e = (Entity<T>) clazz.newInstance();
  30. return e.putAttrs(deserialze(map, clazz));
  31. } catch (Exception e) {
  32. throw new JSONException("Unsupport type " + type, e);
  33. }
  34. }
  35. public int getFastMatchToken() {
  36. return JSONToken.LBRACE;
  37. }
  38. public static Map<String, Object> deserialze(Map<String, Object> map, Class<?> clazz) {
  39. Object obj = null;
  40. Method method = null;
  41. Class<?> returnType = null;
  42. List<Map<String, Object>> list = null;
  43. List<Entity<?>> newlist = null;
  44. JSONArray blist = null;
  45. List<?> newblist = null;
  46. Class returnTypeClass = null;
  47. Set<Entity<?>> newset = null;
  48. JSONArray bset = null;
  49. Set<?> newbset = null;
  50. for (String key : map.keySet()) {
  51. obj = map.get(key);
  52. try {
  53. method = clazz.getDeclaredMethod("get" + Stringer.firstUpperCamelCase(key));
  54. returnType = method.getReturnType();
  55. //如果已经是返回类型 不再处理
  56. if (!obj.getClass().isAssignableFrom(returnType)) {
  57. //如果是String类型
  58. if (String.class.isAssignableFrom(returnType)) {
  59. map.put(key, obj.toString());
  60. } else {
  61. //判断是不是包含 Entity类型
  62. if (obj instanceof JSONObject) {
  63. if (Entity.class.isAssignableFrom(returnType)) {
  64. Entity<?> e = (Entity<?>) returnType.newInstance();
  65. e.putAttrs(deserialze((Map<String, Object>) obj, returnType));
  66. map.put(key, e);
  67. }
  68. } else
  69. //判断是否是Entity的集合类型
  70. if (obj instanceof JSONArray) {
  71. if (Collection.class.isAssignableFrom(returnType)) {
  72. returnTypeClass = (Class<?>) ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
  73. if (List.class.isAssignableFrom(returnType)) {
  74. if (Entity.class.isAssignableFrom(returnTypeClass)) {
  75. list = (List<Map<String, Object>>) obj;
  76. newlist = new ArrayList<Entity<?>>();
  77. for (Map<String, Object> mp : list) {
  78. Entity<?> e = (Entity<?>) returnTypeClass.newInstance();
  79. e.putAttrs(deserialze(mp, returnTypeClass));
  80. newlist.add(e);
  81. }
  82. map.put(key, newlist);
  83. } else {
  84. blist = (JSONArray) obj;
  85. if (String.class.isAssignableFrom(returnTypeClass)) {
  86. newblist = new ArrayList<String>();
  87. for (Object e : blist) {
  88. ((List<String>) newblist).add(e.toString());
  89. }
  90. } else {
  91. newblist = new ArrayList<Object>();
  92. for (Object e : blist) {
  93. if (e.getClass().isAssignableFrom(returnTypeClass))
  94. ((List<Object>) newblist).add(e);
  95. else
  96. ((List<Object>) newblist).add(Jsoner.parseObject(Jsoner.toJSONString(e), returnTypeClass));
  97. }
  98. }
  99. map.put(key, newblist);
  100. }
  101. } else if (Set.class.isAssignableFrom(returnType)) {
  102. if (Entity.class.isAssignableFrom(returnTypeClass)) {
  103. list = (List<Map<String, Object>>) obj;
  104. newset = new HashSet<Entity<?>>();
  105. for (Map<String, Object> mp : list) {
  106. Entity<?> e = (Entity<?>) returnTypeClass.newInstance();
  107. e.putAttrs(deserialze(mp, returnTypeClass));
  108. newset.add(e);
  109. }
  110. map.put(key, newset);
  111. } else {
  112. bset = (JSONArray) obj;
  113. if (String.class.isAssignableFrom(returnTypeClass)) {
  114. newbset = new HashSet<String>();
  115. for (Object e : bset) {
  116. ((Set<String>) newbset).add(e.toString());
  117. }
  118. } else {
  119. newbset = new HashSet<Object>();
  120. for (Object e : bset) {
  121. if (e.getClass().isAssignableFrom(returnTypeClass))
  122. ((Set<Object>) newbset).add(e);
  123. else
  124. ((Set<Object>) newbset).add(Jsoner.parseObject(Jsoner.toJSONString(e), returnTypeClass));
  125. }
  126. }
  127. map.put(key, newbset);
  128. }
  129. }
  130. }
  131. } else {
  132. map.put(key, Jsoner.parseObject(Jsoner.toJSONString(obj), returnType));
  133. }
  134. }
  135. }
  136. } catch (NoSuchMethodException e) {
  137. } catch (Exception e) {
  138. throw new JSONException("Unconvert type " + returnType, e);
  139. }
  140. }
  141. return map;
  142. }
  143. }