JsonUtil.java 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package cn.com.qmth.sdk.util;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import cn.com.qmth.sdk.util.DateUtil.DatePatterns;
  5. /**
  6. * 类注释
  7. *
  8. * @author WANGWEI
  9. */
  10. public class JsonUtil {
  11. /**
  12. * 方法注释
  13. *
  14. * @author WANGWEI
  15. * @param obj
  16. * @return
  17. */
  18. public static String toJson(Object obj) {
  19. Gson gson = new GsonBuilder().disableHtmlEscaping().setDateFormat(DatePatterns.ISO)
  20. .create();
  21. return gson.toJson(obj);
  22. }
  23. /**
  24. * 方法注释
  25. *
  26. * @author WANGWEI
  27. * @param obj
  28. * @return
  29. */
  30. public static String toPrettyJson(Object obj) {
  31. Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
  32. .setDateFormat(DatePatterns.ISO).create();
  33. return gson.toJson(obj);
  34. }
  35. /**
  36. * 方法注释
  37. *
  38. * @author WANGWEI
  39. * @param json
  40. * @param c
  41. * @return
  42. */
  43. public static <T> T fromJson(String json, Class<T> c) {
  44. Gson gson = new GsonBuilder().disableHtmlEscaping().create();
  45. return gson.fromJson(json, c);
  46. }
  47. }