PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src_configDesign/org/hxzon/jt/config/JsonConverter.java

https://gitlab.com/BGCX261/zk-full-demo-git
Java | 229 lines | 205 code | 17 blank | 7 comment | 75 complexity | ff37a74a9376d145989be38b21d05ce9 MD5 | raw file
  1. package org.hxzon.jt.config;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Set;
  6. import org.apache.commons.io.IOUtils;
  7. import org.dom4j.Element;
  8. import org.hxzon.util.Dom4jUtil;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONArray;
  11. import com.alibaba.fastjson.JSONObject;
  12. public class JsonConverter {
  13. private static final Object Null = null;//JSONObject.NULL;
  14. public static OutputSchemaInfo readOutputSchema(String xmlStr) {
  15. Element root = Dom4jUtil.getRoot(xmlStr);
  16. return toOutputSchemaInfo(root);
  17. }
  18. public static OutputSchemaInfo toOutputSchemaInfo(Element e) {
  19. OutputSchemaInfo typeInfo = new OutputSchemaInfo();
  20. typeInfo.setTargetType(e.getName());//myType
  21. typeInfo.setName(Dom4jUtil.getText(e, "@name"));
  22. typeInfo.setFname(Dom4jUtil.getText(e, "@fname"));//source key
  23. typeInfo.setFrom(Dom4jUtil.getText(e, "@from"));
  24. typeInfo.setNotrim(Dom4jUtil.getText(e, "@notrim"));
  25. typeInfo.setVtype(Dom4jUtil.getText(e, "@vtype"));
  26. if (!e.getName().equals("attr")) {
  27. List<OutputSchemaInfo> children = new ArrayList<OutputSchemaInfo>();
  28. List<Element> childrenEle = Dom4jUtil.getElements(e);
  29. for (Element childEle : childrenEle) {
  30. children.add(toOutputSchemaInfo(childEle));
  31. }
  32. typeInfo.setChildren(children);
  33. }
  34. return typeInfo;
  35. }
  36. public static Object convert(Object origObj, OutputSchemaInfo typeInfo) {
  37. if ("attr".equals(typeInfo.getTargetType())) {
  38. return convertAttr(origObj, typeInfo);
  39. } else if ("amap".equals(typeInfo.getTargetType())) {
  40. return convertAmap(origObj, typeInfo);
  41. } else if ("list".equals(typeInfo.getTargetType())) {
  42. return convertList(origObj, typeInfo);
  43. } else if ("map".equals(typeInfo.getTargetType())) {
  44. return convertMap(origObj, typeInfo);
  45. } else {
  46. return convertMap(origObj, typeInfo);//default is map
  47. }
  48. //throw new RuntimeException("myType error");
  49. }
  50. //targetType:map(amap),sourceType:map(amap),valueType:any
  51. public static Object convertAmap(Object origObj, OutputSchemaInfo typeInfo) {
  52. if (origObj == null) {
  53. return Null;
  54. }
  55. if (origObj instanceof JSONObject) {
  56. JSONObject origAmap = (JSONObject) origObj;
  57. JSONObject newAmap = new JSONObject();
  58. Set<String> keys = origAmap.keySet();
  59. for (String key : keys) {
  60. if ("map".equals(typeInfo.getVtype())) {
  61. //origValue is map
  62. JSONObject origValue = (JSONObject) origAmap.get(key);
  63. JSONObject newValue = new JSONObject();
  64. for (OutputSchemaInfo valueTypeInfo : typeInfo.getChildren()) {
  65. Object origValueValue = origValue.get(valueTypeInfo.getFname());
  66. Object newValueValue = convert(origValueValue, valueTypeInfo);
  67. newValue.put(valueTypeInfo.getName(), newValueValue);
  68. }
  69. newAmap.put(key, newValue);
  70. } else {//vtype is simple type
  71. Object origValue = origAmap.get(key);
  72. Object newValue = convertAttr(origValue, typeInfo);
  73. newAmap.put(key, newValue);
  74. }
  75. }
  76. return newAmap;
  77. }
  78. throw new RuntimeException("source type error");
  79. }
  80. //targetType:map,sourceType:map,valueType:any
  81. public static Object convertMap(Object origObj, OutputSchemaInfo typeInfo) {
  82. if (origObj == null) {
  83. return Null;
  84. } else if (origObj instanceof JSONObject) {
  85. JSONObject origMap = (JSONObject) origObj;
  86. JSONObject newMap = new JSONObject();
  87. for (OutputSchemaInfo valueTypeInfo : typeInfo.getChildren()) {
  88. Object origValue = origMap.get(valueTypeInfo.getFname());
  89. Object newValue = convert(origValue, valueTypeInfo);
  90. newMap.put(valueTypeInfo.getName(), newValue);
  91. }
  92. return newMap;
  93. }
  94. throw new RuntimeException("source type error");
  95. }
  96. //targetType:list,sourceType:list or map(amap),childType:any
  97. public static Object convertList(Object origObj, OutputSchemaInfo typeInfo) {
  98. if (origObj == null) {
  99. return Null;
  100. } else if (origObj instanceof JSONArray) {
  101. JSONArray origArray = (JSONArray) origObj;
  102. JSONArray newArray = new JSONArray();
  103. if ("map".equals(typeInfo.getVtype())) {
  104. for (Object origEle : origArray) {
  105. JSONObject origEleMap = (JSONObject) origEle;
  106. JSONObject newEleMap = new JSONObject();
  107. for (OutputSchemaInfo eleValueTypeInfo : typeInfo.getChildren()) {
  108. Object origEleValue = origEleMap.get(eleValueTypeInfo.getFname());
  109. Object newEleValue = convert(origEleValue, eleValueTypeInfo);
  110. newEleMap.put(eleValueTypeInfo.getName(), newEleValue);
  111. }
  112. newArray.add(newEleMap);
  113. }
  114. } else {//vtype is simple type
  115. for (Object origEle : origArray) {
  116. Object newEle = convertAttr(origEle, typeInfo);
  117. newArray.add(newEle);
  118. }
  119. }
  120. return newArray;
  121. } else if (origObj instanceof JSONObject) {
  122. JSONArray newArray = new JSONArray();
  123. JSONObject origAmap = (JSONObject) origObj;
  124. if (typeInfo.haveFrom()) {//vtype is simple type
  125. if ("key".equals(typeInfo.getFrom())) {
  126. for (String key : origAmap.keySet()) {
  127. newArray.add(key);
  128. }
  129. } else if (typeInfo.getFrom().startsWith("value.")) {
  130. for (String key : origAmap.keySet()) {
  131. JSONObject origEleMap = (JSONObject) origAmap.get(key);
  132. String valueKey = typeInfo.getFrom().substring(6);
  133. Object newEle = convertAttr(origEleMap.get(valueKey), typeInfo);
  134. newArray.add(newEle);
  135. }
  136. }
  137. }
  138. return newArray;
  139. }
  140. throw new RuntimeException("source type error");
  141. }
  142. public static Object convertAttr(Object o, OutputSchemaInfo typeInfo) {
  143. if ("string".equals(typeInfo.getVtype())) {
  144. return toString(o, typeInfo);
  145. } else if ("integer".equals(typeInfo.getVtype())) {
  146. return toInteger(o, typeInfo);
  147. } else if ("double".equals(typeInfo.getVtype())) {
  148. return toDouble(o, typeInfo);
  149. } else if ("boolean".equals(typeInfo.getVtype())) {
  150. return toBoolean(o, typeInfo);
  151. }
  152. throw new RuntimeException("is not simple type");
  153. }
  154. //-------------------
  155. public static Object toString(Object o, OutputSchemaInfo typeInfo) {
  156. if (o instanceof String) {
  157. return o;//new MyString((String) o);
  158. } else if (o instanceof Number) {
  159. return o.toString();
  160. } else if (o instanceof Boolean) {
  161. return o.toString();
  162. } else if (typeInfo.haveDefault()) {
  163. return Null;
  164. }
  165. return Null;
  166. }
  167. public static Object toInteger(Object o, OutputSchemaInfo typeInfo) {
  168. if (o instanceof Long) {
  169. return o;
  170. } else if (o instanceof Number) {//int,double
  171. return ((Number) o).longValue();
  172. } else if (o instanceof String) {
  173. return Long.valueOf((String) o);
  174. } else if (typeInfo.haveDefault()) {
  175. return 0L;
  176. }
  177. return Null;
  178. }
  179. public static Object toDouble(Object o, OutputSchemaInfo typeInfo) {
  180. if (o instanceof Double) {
  181. return o;
  182. } else if (o instanceof Number) {//int,long
  183. return ((Number) o).doubleValue();
  184. } else if (o instanceof String) {
  185. return Double.valueOf((String) o);
  186. } else if (typeInfo.haveDefault()) {
  187. return 0.0;
  188. }
  189. return Null;
  190. }
  191. public static Object toBoolean(Object o, OutputSchemaInfo typeInfo) {
  192. if (o instanceof Boolean) {
  193. return o;
  194. } else if (o instanceof Number) {
  195. return ((Number) o).doubleValue() != 0;
  196. } else if (o instanceof String) {
  197. return Boolean.valueOf((String) o);
  198. } else if (typeInfo.haveDefault()) {
  199. return false;
  200. }
  201. return Null;
  202. }
  203. //--------------
  204. public static void main(String args[]) throws IOException {
  205. String jsonS = IOUtils.toString(JsonConverter.class.getResource("test/s18.o"), "utf8");
  206. Object json = JSON.parse(jsonS);
  207. String xmlStr = IOUtils.toString(JsonConverter.class.getResource("test/s18.xml"), "utf8");
  208. OutputSchemaInfo typeInfo = readOutputSchema(xmlStr);
  209. JSONObject newJson = (JSONObject) convert(json, typeInfo);
  210. System.out.println(JSON.toJSONString(newJson, true));
  211. }
  212. }