JSON.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.sooka.common.json;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import com.fasterxml.jackson.core.JsonGenerationException;
  7. import com.fasterxml.jackson.core.JsonParseException;
  8. import com.fasterxml.jackson.databind.JsonMappingException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.fasterxml.jackson.databind.ObjectWriter;
  11. /**
  12. * JSON解析处理
  13. *
  14. * @author lei_wang
  15. */
  16. public class JSON
  17. {
  18. public static final String DEFAULT_FAIL = "\"Parse failed\"";
  19. private static final ObjectMapper objectMapper = new ObjectMapper();
  20. private static final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
  21. public static void marshal(File file, Object value) throws Exception
  22. {
  23. try
  24. {
  25. objectWriter.writeValue(file, value);
  26. }
  27. catch (JsonGenerationException e)
  28. {
  29. throw new Exception(e);
  30. }
  31. catch (JsonMappingException e)
  32. {
  33. throw new Exception(e);
  34. }
  35. catch (IOException e)
  36. {
  37. throw new Exception(e);
  38. }
  39. }
  40. public static void marshal(OutputStream os, Object value) throws Exception
  41. {
  42. try
  43. {
  44. objectWriter.writeValue(os, value);
  45. }
  46. catch (JsonGenerationException e)
  47. {
  48. throw new Exception(e);
  49. }
  50. catch (JsonMappingException e)
  51. {
  52. throw new Exception(e);
  53. }
  54. catch (IOException e)
  55. {
  56. throw new Exception(e);
  57. }
  58. }
  59. public static String marshal(Object value) throws Exception
  60. {
  61. try
  62. {
  63. return objectWriter.writeValueAsString(value);
  64. }
  65. catch (JsonGenerationException e)
  66. {
  67. throw new Exception(e);
  68. }
  69. catch (JsonMappingException e)
  70. {
  71. throw new Exception(e);
  72. }
  73. catch (IOException e)
  74. {
  75. throw new Exception(e);
  76. }
  77. }
  78. public static byte[] marshalBytes(Object value) throws Exception
  79. {
  80. try
  81. {
  82. return objectWriter.writeValueAsBytes(value);
  83. }
  84. catch (JsonGenerationException e)
  85. {
  86. throw new Exception(e);
  87. }
  88. catch (JsonMappingException e)
  89. {
  90. throw new Exception(e);
  91. }
  92. catch (IOException e)
  93. {
  94. throw new Exception(e);
  95. }
  96. }
  97. public static <T> T unmarshal(File file, Class<T> valueType) throws Exception
  98. {
  99. try
  100. {
  101. return objectMapper.readValue(file, valueType);
  102. }
  103. catch (JsonParseException e)
  104. {
  105. throw new Exception(e);
  106. }
  107. catch (JsonMappingException e)
  108. {
  109. throw new Exception(e);
  110. }
  111. catch (IOException e)
  112. {
  113. throw new Exception(e);
  114. }
  115. }
  116. public static <T> T unmarshal(InputStream is, Class<T> valueType) throws Exception
  117. {
  118. try
  119. {
  120. return objectMapper.readValue(is, valueType);
  121. }
  122. catch (JsonParseException e)
  123. {
  124. throw new Exception(e);
  125. }
  126. catch (JsonMappingException e)
  127. {
  128. throw new Exception(e);
  129. }
  130. catch (IOException e)
  131. {
  132. throw new Exception(e);
  133. }
  134. }
  135. public static <T> T unmarshal(String str, Class<T> valueType) throws Exception
  136. {
  137. try
  138. {
  139. return objectMapper.readValue(str, valueType);
  140. }
  141. catch (JsonParseException e)
  142. {
  143. throw new Exception(e);
  144. }
  145. catch (JsonMappingException e)
  146. {
  147. throw new Exception(e);
  148. }
  149. catch (IOException e)
  150. {
  151. throw new Exception(e);
  152. }
  153. }
  154. public static <T> T unmarshal(byte[] bytes, Class<T> valueType) throws Exception
  155. {
  156. try
  157. {
  158. if (bytes == null)
  159. {
  160. bytes = new byte[0];
  161. }
  162. return objectMapper.readValue(bytes, 0, bytes.length, valueType);
  163. }
  164. catch (JsonParseException e)
  165. {
  166. throw new Exception(e);
  167. }
  168. catch (JsonMappingException e)
  169. {
  170. throw new Exception(e);
  171. }
  172. catch (IOException e)
  173. {
  174. throw new Exception(e);
  175. }
  176. }
  177. }