在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

jackson工具类

2016-12-20 13:16| 发布者: zhangjf| 查看: 463| 评论: 0

摘要: jackson工具类 没有闭门造车.解决了lazyload的问题 JpaLazyIntrospector.java package com.rf.dtd.systemsupport.reportmodel.Util;import java.lang.annotation.Annotation;import javax. ...
jackson工具类
没有闭门造车.解决了lazyload的问题

JpaLazyIntrospector.java
  1. package com.rf.dtd.systemsupport.reportmodel.Util;
  2. import java.lang.annotation.Annotation;
  3. import javax.persistence.Basic;
  4. import javax.persistence.ElementCollection;
  5. import javax.persistence.FetchType;
  6. import javax.persistence.ManyToMany;
  7. import javax.persistence.ManyToOne;
  8. import javax.persistence.OneToMany;
  9. import javax.persistence.OneToOne;
  10. import org.codehaus.jackson.map.introspect.Annotated;
  11. import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
  12. class JpaLazyIntrospector extends JacksonAnnotationIntrospector
  13. {
  14. public boolean isHandled(Annotation ann)
  15. {
  16. boolean handled = super.isHandled(ann);
  17. if (!handled) {
  18. handled = ann.toString().startsWith("@javax.persistence");
  19. }
  20. if (!handled) {
  21. handled = ann.annotationType().equals(JsonLazy.class);
  22. }
  23. return handled;
  24. }
  25. protected boolean _isIgnorable(Annotated a)
  26. {
  27. boolean ignor = super._isIgnorable(a);
  28. if (!ignor) {
  29. ignor = isLazy(a);
  30. }
  31. return ignor;
  32. }
  33. private boolean isLazy(Annotated a) {
  34. boolean lazy = false;
  35. JsonLazy jsonLazy = (JsonLazy)a.getAnnotation(JsonLazy.class);
  36. if (jsonLazy != null) {
  37. return false;
  38. }
  39. Basic basic = (Basic)a.getAnnotation(Basic.class);
  40. if ((basic != null) && (basic.fetch() == FetchType.LAZY)) {
  41. lazy = true;
  42. }
  43. ElementCollection elementCollection = (ElementCollection)a.getAnnotation(ElementCollection.class);
  44. if ((elementCollection != null) && (elementCollection.fetch() != FetchType.EAGER)) {
  45. lazy = true;
  46. }
  47. ManyToMany manyToMany = (ManyToMany)a.getAnnotation(ManyToMany.class);
  48. if ((manyToMany != null) && (manyToMany.fetch() != FetchType.EAGER)) {
  49. lazy = true;
  50. }
  51. OneToMany oneToMany = (OneToMany)a.getAnnotation(OneToMany.class);
  52. if ((oneToMany != null) && (oneToMany.fetch() != FetchType.EAGER)) {
  53. lazy = true;
  54. }
  55. ManyToOne manyToOne = (ManyToOne)a.getAnnotation(ManyToOne.class);
  56. if ((manyToOne != null) && (manyToOne.fetch() == FetchType.LAZY)) {
  57. lazy = true;
  58. }
  59. OneToOne oneToOne = (OneToOne)a.getAnnotation(OneToOne.class);
  60. if ((oneToOne != null) && (oneToOne.fetch() == FetchType.LAZY)) {
  61. lazy = true;
  62. }
  63. return lazy;
  64. }
  65. }
复制代码
JsonLazy.java
  1. package com.rf.dtd.systemsupport.reportmodel.Util;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD})
  6. @Retention(RetentionPolicy.RUNTIME)
  7. public @interface JsonLazy
  8. {
  9. public abstract boolean value();
  10. }
复制代码
JsonUtil.java ~ 9KB
  1. package com.rf.dtd.systemsupport.reportmodel.Util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.Writer;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.LinkedHashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import org.codehaus.jackson.JsonNode;
  14. import org.codehaus.jackson.JsonParser;
  15. import org.codehaus.jackson.map.DeserializationConfig;
  16. import org.codehaus.jackson.map.ObjectMapper;
  17. import org.codehaus.jackson.map.SerializationConfig;
  18. import org.codehaus.jackson.node.TextNode;
  19. import org.codehaus.jackson.type.TypeReference;
  20. public abstract class JsonUtil
  21. {
  22. public static void main(String[] args)
  23. {
  24. Object ss = parseJson("{"datasourceId":12201,"dsName":"bj","jndiName":"","driverClass":"oracle.jdbc.OracleDriver","jdbcURL":"jdbc:oracle:thin:@10.88.107.250:1521:orcl","userName":"devtest","password":"devtest"}");
  25. String josnString = "{ "excelName" : "组织结构模板.xls", "sheet" : [ { "entryCode" : "epdm_pcOrganizationT", "columns" : [ { "field" : "orgName", "title" : "机构名称" }, { "field" : "orgCode", "title" : "机构代码" }, { "field" : "canton", "title" : "行政区名称" }, { "field" : "cantonCode", "title" : "行政区代码" }, { "field" : "mailingAddress", "title" : "邮递地址" }, { "field" : "orgId", "title" : "ORG_ID" }, { "field" : "orgLevel", "title" : "级别" }, { "field" : "parentId", "title" : "上级机构名称" } ], "sheetName" : "Sheet1", "rows" : [ { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "9999.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "10000.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "10001.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "10002.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" } ] }, { "entryCode" : "epdm_pcOrganizationT", "columns" : [ { "field" : "orgName", "title" : "机构名称" }, { "field" : "orgCode", "title" : "机构代码" }, { "field" : "canton", "title" : "行政区名称" }, { "field" : "cantonCode", "title" : "行政区代码" }, { "field" : "mailingAddress", "title" : "邮递地址" }, { "field" : "orgId", "title" : "ORG_ID" }, { "field" : "orgLevel", "title" : "级别" }, { "field" : "parentId", "title" : "上级机构名称" } ], "sheetName" : "Sheet2", "rows" : [ { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "888.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "889.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "890.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" }, { "" : "", "orgId" : "444.0", "parentId" : "雍和宫", "cantonCode" : "7777.0", "orgCode" : "891.0", "canton" : "石景山", "orgName" : "八角", "mailingAddress" : "102300.0", "orgLevel" : "中级" } ] } ] }";
  26. Object sssw = parseJson(josnString);
  27. System.out.println(sssw);
  28. System.out.println(ss);
  29. }
  30. public static String getJSONString(String filePath) throws Exception{
  31. StringBuffer data = new StringBuffer();
  32. BufferedReader reader = null;
  33. reader = new BufferedReader(new FileReader(new File(filePath)));
  34. String temp = null;
  35. while((temp = reader.readLine()) != null){
  36. data.append(temp);
  37. }
  38. if (reader != null){
  39. reader.close();
  40. }
  41. return data.toString();
  42. }
  43. public static ObjectMapper getObjectMapper() {
  44. ObjectMapper om = new ObjectMapper().setAnnotationIntrospector(new JpaLazyIntrospector());
  45. om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  46. om.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
  47. om.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
  48. om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  49. om.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
  50. om.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
  51. om.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
  52. om.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
  53. om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
  54. return om;
  55. }
  56. public static String toJsonString(Object object)
  57. {
  58. try
  59. {
  60. return getObjectMapper().writeValueAsString(object);
  61. } catch (IOException ex) {
  62. throw new RuntimeException(ex);
  63. }
  64. }
  65. public static void writeJsonString(Object object, Writer writer)
  66. {
  67. try
  68. {
  69. getObjectMapper().writeValue(writer, object);
  70. } catch (IOException ex) {
  71. throw new RuntimeException(ex);
  72. }
  73. }
  74. public static Map<?, ?> parseJson(String jsonString)
  75. {
  76. JsonNode jn = null;
  77. try {
  78. jn = getObjectMapper().readTree(jsonString);
  79. } catch (IOException ex) {
  80. ex.printStackTrace();
  81. }
  82. return (Map<?, ?>)JsonNodeToMap(jn);
  83. }
  84. public static Object parseJson2MapOrList(String jsonString)
  85. {
  86. JsonNode jn = null;
  87. try {
  88. jn = getObjectMapper().readTree(jsonString);
  89. } catch (IOException ex) {
  90. ex.printStackTrace();
  91. }
  92. return JsonNodeToMap(jn);
  93. }
  94. public static <T> T parseJson(String jsonString, Class<T> classType)
  95. {
  96. try {
  97. return getObjectMapper().readValue(jsonString, classType);
  98. } catch (Exception ex) {
  99. ex.printStackTrace();
  100. }return null;
  101. }
  102. public static <T> T parseJson(String jsonString, TypeReference<T> typeReference)
  103. {
  104. try
  105. {
  106. return getObjectMapper().readValue(jsonString, typeReference);
  107. } catch (Exception ex) {
  108. ex.printStackTrace();
  109. }return null;
  110. }
  111. public static <T> T parseJsonT(String jsonString)
  112. {
  113. try
  114. {
  115. return getObjectMapper().readValue(jsonString, new TypeReference<Object>() { } );
  116. }
  117. catch (Exception ex) {
  118. ex.printStackTrace();
  119. }return null;
  120. }
  121. public static <T> Map<?, ?> bean2Map(Object bean)
  122. {
  123. try
  124. {
  125. return (Map<?, ?>)getObjectMapper().convertValue(bean, Map.class);
  126. } catch (Exception ex) {
  127. ex.printStackTrace();
  128. }return null;
  129. }
  130. public static <T> T map2Bean(Map<?, ?> map, Class<T> clazz)
  131. {
  132. try
  133. {
  134. return getObjectMapper().convertValue(map, clazz);
  135. } catch (Exception ex) {
  136. ex.printStackTrace();
  137. }return null;
  138. }
  139. public static Object JsonNodeToMap(JsonNode root)
  140. {
  141. Map<String, Object> map = new LinkedHashMap<String, Object>();
  142. if (root == null) {
  143. return map;
  144. }
  145. if (root.isArray()) {
  146. List<Object> list = new ArrayList<Object>();
  147. for (JsonNode node : root) {
  148. Object nmp = JsonNodeToMap(node);
  149. list.add(nmp);
  150. }
  151. return list;
  152. }if (root.isTextual()) {
  153. try {
  154. return ((TextNode)root).asText();
  155. } catch (Exception e) {
  156. return root.toString();
  157. }
  158. }
  159. Iterator<?> iter = root.getFields();
  160. while (iter.hasNext()) {
  161. @SuppressWarnings("rawtypes")
  162. Map.Entry entry = (Map.Entry)iter.next();
  163. String key = (String)entry.getKey();
  164. JsonNode ele = (JsonNode)entry.getValue();
  165. if (ele.isObject())
  166. map.put(key, JsonNodeToMap(ele));
  167. else if (ele.isTextual())
  168. map.put(key, ((TextNode)ele).asText());
  169. else if (ele.isArray())
  170. map.put(key, JsonNodeToMap(ele));
  171. else {
  172. map.put(key, ele.toString());
  173. }
  174. }
  175. return map;
  176. }
  177. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 17:28

Copyright 2015-2025 djqfx

返回顶部