JsonUtil.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.ym.mec.util.json;
  2. import java.lang.reflect.Type;
  3. import java.math.BigDecimal;
  4. import java.nio.charset.CharsetDecoder;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.alibaba.fastjson.parser.Feature;
  12. import com.alibaba.fastjson.parser.ParserConfig;
  13. import com.alibaba.fastjson.serializer.JSONSerializer;
  14. import com.alibaba.fastjson.serializer.SerializeConfig;
  15. import com.alibaba.fastjson.serializer.SerializeWriter;
  16. import com.alibaba.fastjson.serializer.SerializerFeature;
  17. import com.alibaba.fastjson.serializer.ValueFilter;
  18. import com.alibaba.fastjson.util.TypeUtils;
  19. /**
  20. * 注意点: 如果存在对象间循环引用问题,解决办法: 将get方法的方法名非标准的名称,
  21. * 例如将getProductCategory()改成getproductCategory() 或者将属性定义加上transient关键字
  22. * 最后输出的json串中不包含该对象的信息 OMS系统: solar2.utils 创建者: gaoyong 创建时间: 2013-6-21
  23. */
  24. public class JsonUtil {
  25. private static SerializeConfig config = new SerializeConfig();
  26. public static SerializeConfig getConfig() {
  27. return config;
  28. }
  29. /**
  30. * 将json字符串反序列化为指定对象
  31. *
  32. * @param <T>
  33. * @param text
  34. * json串
  35. * @param clazz
  36. * 对象的类型
  37. * @param features
  38. * 特性
  39. * @return 特定对象
  40. */
  41. public static <T> T parseObject(String text, Class<T> clazz, Feature... features) {
  42. return JSON.parseObject(text, clazz, features);
  43. }
  44. /**
  45. * 将集合型json串转换成集合对象
  46. *
  47. * @param <T>
  48. * @param text
  49. * json串
  50. * @param clazz
  51. * 泛型类型
  52. * @return 特定对象的集合
  53. */
  54. public static <T> List<T> parseArray(String text, Class<T> clazz) {
  55. return JSON.parseArray(text, clazz);
  56. }
  57. /**
  58. * 将字节数组转换成指定对象
  59. *
  60. * @param <T>
  61. * @param input
  62. * 需要转换的内容
  63. * @param clazz
  64. * 对象类型
  65. * @param charsetDecoder
  66. * 解码方式
  67. * @param features
  68. * 特性
  69. * @return 特定对象
  70. */
  71. public static <T> T parseObject(byte[] input, Type clazz, CharsetDecoder charsetDecoder, Feature... features) {
  72. return JSON.parseObject(input, 0, input.length, charsetDecoder, clazz, features);
  73. }
  74. /**
  75. * 忽略具体对象中的某些属性,将其他属性转换成json字符串
  76. *
  77. * @param object
  78. * 具体对象
  79. * @param ingnoreProperties
  80. * 忽略的属性名称,注意大小写
  81. * @return json字符串
  82. */
  83. public static String toJsonStringWithIngnore(Object object, final List<String> ingnoreProperties) {
  84. ValueFilter filter = new ValueFilter() {
  85. public Object process(Object object, String property, Object propertyValue) {
  86. if (ingnoreProperties != null) {
  87. if (ingnoreProperties.contains(property)) {
  88. return null;
  89. }
  90. }
  91. return propertyValue;
  92. }
  93. };
  94. return toJsonString(object, filter);
  95. }
  96. /**
  97. * 允许具体对象中的某些属性对应的值转换成json字符串
  98. *
  99. * @param object
  100. * 具体对象
  101. * @param allowProperties
  102. * 允许转换成json字符串的属性值对应的属性名称,注意大小写
  103. * @return json字符串
  104. */
  105. public static String toJsonStringWithAllow(Object object, final List<String> allowProperties) {
  106. ValueFilter filter = new ValueFilter() {
  107. public Object process(Object object, String property, Object propertyValue) {
  108. if (allowProperties != null) {
  109. if (allowProperties.contains(property)) {
  110. return propertyValue;
  111. }
  112. }
  113. return null;
  114. }
  115. };
  116. return toJsonString(object, filter);
  117. }
  118. private static String toJsonString(Object obj, ValueFilter filter) {
  119. SerializeWriter out = new SerializeWriter(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.QuoteFieldNames,
  120. SerializerFeature.SkipTransientField, SerializerFeature.WriteEnumUsingToString, SerializerFeature.SortField);
  121. JSONSerializer serializer = new JSONSerializer(out);
  122. serializer.config(SerializerFeature.WriteEnumUsingToString, false);
  123. serializer.getValueFilters().add(filter);
  124. serializer.write(obj);
  125. return out.toString();
  126. }
  127. /**
  128. * 将传入的List&lt;JSONObject&gt;改为List&lt;Object&gt;
  129. *
  130. * @param jsonList
  131. * 传入的List&lt;JSONObject&gt;
  132. * @param clazz
  133. * 转换的类
  134. * @return List&lt;Object&gt;
  135. */
  136. public static <T> List<T> convJOSNList2ObjList(List<JSONObject> jsonList, Class<T> clazz) {
  137. List<T> objList = new ArrayList<T>();
  138. if (jsonList != null && jsonList.size() > 0) {
  139. for (int i = 0; i < jsonList.size(); i++) {
  140. T val = TypeUtils.castToJavaBean(jsonList.get(i), clazz);
  141. objList.add(val);
  142. }
  143. }
  144. return objList;
  145. }
  146. public static String toJSONString(Object object, SerializerFeature... features) {
  147. return toJSONString(object, config, null, features);
  148. }
  149. public static String toJSONString(Object object, SerializeConfig config, SerializerFeature... features) {
  150. return toJSONString(object, config, null, features);
  151. }
  152. public static String toJSONString(Object object, ValueFilter filter, SerializerFeature... features) {
  153. return toJSONString(object, config, filter, features);
  154. }
  155. public static String toJSONString(Object object, SerializeConfig config, ValueFilter filter, SerializerFeature... features) {
  156. SerializeWriter out = new SerializeWriter(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteNullStringAsEmpty,
  157. SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames, SerializerFeature.WriteNullNumberAsZero);
  158. JSONSerializer serializer = new JSONSerializer(out, config);
  159. for (SerializerFeature feature : features) {
  160. serializer.config(feature, true);
  161. }
  162. if (filter == null) {
  163. filter = new ValueFilter() {
  164. @Override
  165. public Object process(Object object, String name, Object value) {
  166. if (value instanceof BigDecimal || value instanceof Double || value instanceof Float) {
  167. return new BigDecimal(value.toString());
  168. }
  169. return value;
  170. }
  171. };
  172. }
  173. serializer.getValueFilters().add(filter);
  174. serializer.write(object);
  175. return out.toString();
  176. }
  177. /**
  178. * 可根据配置参数对目标对象进行转换成json字符串
  179. *
  180. * @param obj
  181. * 目标对象
  182. * @param configs
  183. * 序列化配置
  184. * @param configs
  185. * 特征配置
  186. * @return
  187. */
  188. public static String toJsonString(Object obj, SerializeConfig config, Map<SerializerFeature, Boolean> configs) {
  189. SerializeWriter out = new SerializeWriter();
  190. JSONSerializer serializer = new JSONSerializer(out, config);
  191. if (configs != null && configs.size() > 0) {
  192. for (Entry<SerializerFeature, Boolean> entry : configs.entrySet()) {
  193. serializer.config(entry.getKey(), entry.getValue());
  194. }
  195. }
  196. serializer.write(obj);
  197. return out.toString();
  198. }
  199. /**
  200. * Map转换成对象
  201. * @param map 数据源
  202. * @param clazz 对象类型
  203. * @return
  204. */
  205. public static <T> T toJavaObject(Map<String, Object> map, Class<T> clazz) {
  206. return TypeUtils.cast(new JSONObject(map), clazz, ParserConfig.getGlobalInstance());
  207. }
  208. }