JSONUtils.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.yihu.jw.utils;
  2. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  3. import com.fasterxml.jackson.core.JsonParser.Feature;
  4. import com.fasterxml.jackson.databind.DeserializationFeature;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.SerializationFeature;
  7. import org.apache.commons.lang3.StringUtils;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.Reader;
  11. import java.io.Writer;
  12. import java.text.SimpleDateFormat;
  13. public class JSONUtils
  14. {
  15. private static ObjectMapper mapper = new ObjectMapper();
  16. static
  17. {
  18. mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  19. mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
  20. mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
  21. mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
  22. mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  23. mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  24. mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  25. mapper.setSerializationInclusion(Include.NON_NULL);
  26. }
  27. public static <T> T parse(String value, Class<T> clz)
  28. {
  29. if (StringUtils.isEmpty(value)) {
  30. return null;
  31. }
  32. try
  33. {
  34. return (T)mapper.readValue(value, clz);
  35. }
  36. catch (Exception e)
  37. {
  38. throw new IllegalStateException(e);
  39. }
  40. }
  41. public static <T> T parse(byte[] bytes, Class<T> clz)
  42. {
  43. try
  44. {
  45. return (T)mapper.readValue(bytes, clz);
  46. }
  47. catch (Exception e)
  48. {
  49. throw new IllegalStateException(e);
  50. }
  51. }
  52. public static <T> T parse(InputStream ins, Class<T> clz)
  53. {
  54. try
  55. {
  56. return (T)mapper.readValue(ins, clz);
  57. }
  58. catch (Exception e)
  59. {
  60. throw new IllegalStateException(e);
  61. }
  62. }
  63. public static <T> T parse(Reader reader, Class<T> clz)
  64. {
  65. try
  66. {
  67. return (T)mapper.readValue(reader, clz);
  68. }
  69. catch (Exception e)
  70. {
  71. throw new IllegalStateException(e);
  72. }
  73. }
  74. public static <T> T update(String value, T object)
  75. {
  76. try
  77. {
  78. return (T)mapper.readerForUpdating(object).readValue(value);
  79. }
  80. catch (Exception e)
  81. {
  82. throw new IllegalStateException(e);
  83. }
  84. }
  85. public static String writeValueAsString(Object o)
  86. {
  87. try
  88. {
  89. return mapper.writeValueAsString(o);
  90. }
  91. catch (Exception e)
  92. {
  93. throw new IllegalStateException(e);
  94. }
  95. }
  96. public static void write(OutputStream outs, Object o)
  97. {
  98. try
  99. {
  100. mapper.writeValue(outs, o);
  101. }
  102. catch (Exception e)
  103. {
  104. throw new IllegalStateException(e);
  105. }
  106. }
  107. public static void write(Writer writer, Object o)
  108. {
  109. try
  110. {
  111. mapper.writeValue(writer, o);
  112. }
  113. catch (Exception e)
  114. {
  115. throw new IllegalStateException(e);
  116. }
  117. }
  118. public static String toString(Object o)
  119. {
  120. try
  121. {
  122. return mapper.writeValueAsString(o);
  123. }
  124. catch (Exception e)
  125. {
  126. throw new IllegalStateException(e);
  127. }
  128. }
  129. public static String toString(Object o, Class<?> clz)
  130. {
  131. try
  132. {
  133. return mapper.writerFor(clz).writeValueAsString(o);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new IllegalStateException(e);
  138. }
  139. }
  140. public static byte[] toBytes(Object o)
  141. {
  142. try
  143. {
  144. return mapper.writeValueAsBytes(o);
  145. }
  146. catch (Exception e)
  147. {
  148. throw new IllegalStateException(e);
  149. }
  150. }
  151. }