JsonUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //package com.example.utils;
  2. //import java.lang.reflect.Type;
  3. //import java.util.ArrayList;
  4. //import java.util.List;
  5. //
  6. //import com.alibaba.fastjson.JSON;
  7. //import com.alibaba.fastjson.JSONObject;
  8. //import com.alibaba.fastjson.serializer.SerializerFeature;
  9. //
  10. //import cn.hutool.core.lang.TypeReference;
  11. //import cn.hutool.core.util.TypeUtil;
  12. //
  13. ///**
  14. // * @Title: JsonUtil
  15. // * @Description: Json工具类,暂时内部使用fastjson实现
  16. // * @version V1.0
  17. // */
  18. //public class JsonUtil {
  19. // private JsonUtil() {}
  20. //
  21. // public static String obj2json(Object obj) {
  22. // return obj2json(obj, DateUtil.DATE_TIME_PATTERN);
  23. // }
  24. //
  25. // /**
  26. // *
  27. // * @Description:
  28. // * @param obj
  29. // * @param beIgnoreNull 是否忽略null值
  30. // * @param beFormat 是否格式化
  31. // * @param dataFormat 指定日期格式化
  32. // * @return
  33. // */
  34. // public static String obj2json2(Object obj, boolean beIgnoreNull, boolean beFormat, String dataFormat) {
  35. // List<SerializerFeature> features = new ArrayList<>();
  36. // if (!beIgnoreNull) {
  37. // features.add(SerializerFeature.WriteMapNullValue);
  38. // features.add(SerializerFeature.DisableCircularReferenceDetect);
  39. // features.add(SerializerFeature.WriteNullStringAsEmpty);
  40. // features.add(SerializerFeature.WriteNullListAsEmpty);
  41. // features.add(SerializerFeature.WriteDateUseDateFormat);
  42. // }
  43. // if (beFormat) {
  44. // features.add(SerializerFeature.PrettyFormat);
  45. // }
  46. // features.add(SerializerFeature.WriteDateUseDateFormat);
  47. // return obj2json2(obj, dataFormat, features.toArray(new SerializerFeature[] {}));
  48. // }
  49. //
  50. // public static String obj2json2(Object obj, boolean beIgnoreNull, boolean beFormat) {
  51. // return obj2json2(obj, beIgnoreNull, beFormat, DateUtil.DATE_TIME_PATTERN);
  52. // }
  53. //
  54. // public static String obj2json2(Object obj, boolean beIgnoreNull) {
  55. // return obj2json2(obj, beIgnoreNull, false);
  56. // }
  57. //
  58. // public static String obj2json2(Object obj) {
  59. // return obj2json2(obj, false);
  60. // }
  61. //
  62. // /**
  63. // *
  64. // * @Description: 对象转json
  65. // * @param obj
  66. // * 对象
  67. // * @param beFormat
  68. // * 是否格式化
  69. // * @return json字符串
  70. // */
  71. // public static String obj2json(Object obj, boolean beFormat) {
  72. // return obj2json2(obj, false, beFormat);
  73. // }
  74. //
  75. // /**
  76. // *
  77. // * @Description: 对象转json
  78. // * @param obj
  79. // * @param dataFormat 日期格式
  80. // * @return
  81. // *
  82. // * 如果java bean的Date类型属性需要特殊处理,使用注解
  83. // * @com.alibaba.fastjson.annotation.JSONField
  84. // */
  85. // public static String obj2json(Object obj, String dataFormat) {
  86. // return obj2json2(obj, false, false, dataFormat);
  87. // }
  88. //
  89. // private static String obj2json2(Object obj, String dataFormat, SerializerFeature... feature) {
  90. // String defaultFormat = JSONObject.DEFFAULT_DATE_FORMAT;
  91. // JSONObject.DEFFAULT_DATE_FORMAT = dataFormat;
  92. // String json = JSON.toJSONString(obj, feature);
  93. // JSONObject.DEFFAULT_DATE_FORMAT = defaultFormat;
  94. // return json;
  95. // }
  96. //
  97. // /**
  98. // *
  99. // * @Description: xml转json
  100. // * @param xml
  101. // * @return json字符串
  102. // */
  103. // public static String xml2json(String xml) {
  104. // return cn.hutool.json.JSONUtil.xmlToJson(xml).toJSONString(0);
  105. // }
  106. //
  107. // /**
  108. // *
  109. // * @Description: json转对象
  110. // * @param jsonString
  111. // * @param beanClass
  112. // * @return 实体类对象
  113. // */
  114. // public static <T> T json2Obj(String jsonString, Class<T> beanClass) {
  115. // return JSON.parseObject(jsonString, beanClass);
  116. // }
  117. //
  118. // /**
  119. // *
  120. // * @Description JSON字符串转为实体类对象,转换异常将被抛出
  121. // *
  122. // * @param <T>
  123. // * Bean类型
  124. // * @param jsonString
  125. // * JSON字符串
  126. // * @param typeReference
  127. // * {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
  128. // * @param ignoreError
  129. // * 是否忽略错误
  130. // * @return 实体类对象
  131. // */
  132. // public static <T> T json2Obj(String jsonString, com.sf.nwms.core.util.lang.TypeReference<T> typeReference,
  133. // boolean ignoreError) {
  134. // /* return cn.hutool.json.JSONUtil.toBean(jsonString, TypeUtil.getTypeArgument(typeReference.getClass()),
  135. // ignoreError);*/
  136. // return json2Obj(jsonString, typeReference);
  137. // }
  138. //
  139. // /**
  140. // *
  141. // * @Description:
  142. // * @param <T>
  143. // * @param jsonString
  144. // * @param typeReference
  145. // * @return
  146. // *
  147. // * eg: JsonUtil.json2Obj(jsonString, new com.sf.nwms.core.util.lang.TypeReference<List<JavaBean>())
  148. // */
  149. // public static <T> T json2Obj(String jsonString, com.sf.nwms.core.util.lang.TypeReference<T> typeReference) {
  150. // return JSON.parseObject(jsonString, TypeUtil.getTypeArgument(typeReference.getClass()));
  151. // }
  152. //
  153. // /**
  154. // *
  155. // * @Description: json转对象
  156. // * @param jsonString
  157. // * json字符串
  158. // * @param beanType
  159. // * 实体类对象类型
  160. // * @param ignoreError
  161. // * 是否忽略错误
  162. // * @return 实体类对象
  163. // */
  164. // public static <T> T json2Obj(String jsonString, Type beanType, boolean ignoreError) {
  165. // return JSON.parseObject(jsonString, beanType);
  166. // }
  167. //
  168. //}