JsonUtil.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package cn.com.qmth.examcloud.commons.util;
  2. import java.lang.reflect.Type;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import com.google.common.collect.Maps;
  9. import com.google.gson.ExclusionStrategy;
  10. import com.google.gson.FieldAttributes;
  11. import com.google.gson.Gson;
  12. import com.google.gson.GsonBuilder;
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonObject;
  15. import com.google.gson.JsonParser;
  16. import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns;
  17. /**
  18. * json tool
  19. *
  20. * @author WANGWEI
  21. * @param <T>
  22. * @date 2019年1月16日
  23. * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  24. */
  25. public class JsonUtil {
  26. /**
  27. * to json
  28. *
  29. * @author WANGWEI
  30. * @param obj
  31. * @return
  32. */
  33. public static String toJson(Object obj) {
  34. GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().serializeNulls()
  35. .setDateFormat(DatePatterns.CHINA_DEFAULT);
  36. return builder.create().toJson(obj);
  37. }
  38. /**
  39. * 方法注释
  40. *
  41. * @author WANGWEI
  42. * @param obj
  43. * @param excludeFields
  44. * @return
  45. */
  46. public static String toJson(Object obj, String... excludeFields) {
  47. GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().serializeNulls()
  48. .setDateFormat(DatePatterns.CHINA_DEFAULT);
  49. builder.setExclusionStrategies(new ExclusionStrategy() {
  50. @Override
  51. public boolean shouldSkipField(FieldAttributes f) {
  52. for (String field : excludeFields) {
  53. if (f.getName().matches(field)) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. @Override
  60. public boolean shouldSkipClass(Class<?> clazz) {
  61. return false;
  62. }
  63. });
  64. return builder.create().toJson(obj);
  65. }
  66. /**
  67. * to pretty json
  68. *
  69. * @author WANGWEI
  70. * @param obj
  71. * @return
  72. */
  73. public static String toPrettyJson(Object obj) {
  74. GsonBuilder builder = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
  75. .serializeNulls().setDateFormat(DatePatterns.CHINA_DEFAULT);
  76. return builder.create().toJson(obj);
  77. }
  78. /**
  79. * json转对象
  80. *
  81. * @author WANGWEI
  82. * @param json
  83. * @param c
  84. * @return
  85. */
  86. public static <T> T fromJson(String json, Class<T> c) {
  87. Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
  88. .setDateFormat(DatePatterns.CHINA_DEFAULT).create();
  89. return gson.fromJson(json, c);
  90. }
  91. /**
  92. * json数组转list
  93. *
  94. * @author WANGWEI
  95. * @param json
  96. * @param elementType
  97. * @return
  98. */
  99. public static <T> List<T> fromJsonArray(String json, Class<T[]> elementType) {
  100. Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
  101. .setDateFormat(DatePatterns.CHINA_DEFAULT).create();
  102. T[] array = gson.fromJson(json, elementType);
  103. return Arrays.asList(array);
  104. }
  105. /**
  106. * simple json to map
  107. *
  108. * @author WANGWEI
  109. * @param s
  110. * @return
  111. */
  112. public static Map<String, String> json2Map(String s) {
  113. JsonParser parser = new JsonParser();
  114. JsonElement jsonElement = parser.parse(s);
  115. JsonObject jsonObject = jsonElement.getAsJsonObject();
  116. Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
  117. Map<String, String> map = Maps.newHashMap();
  118. for (Entry<String, JsonElement> entry : entrySet) {
  119. JsonElement e = entry.getValue();
  120. if (!e.isJsonNull()) {
  121. String v = e.getAsString();
  122. map.put(entry.getKey(), v);
  123. }
  124. }
  125. return map;
  126. }
  127. /**
  128. * json转对象
  129. *
  130. * @author WANGWEI
  131. * @param json
  132. * @param type
  133. * @return
  134. */
  135. public static <T> T fromJson(String json, Type type) {
  136. Gson gson = new GsonBuilder().disableHtmlEscaping()
  137. .setDateFormat(DatePatterns.CHINA_DEFAULT).create();
  138. return gson.fromJson(json, type);
  139. }
  140. }