Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. import com.fasterxml.jackson.core.JsonGenerator.Feature;
  2. import com.fasterxml.jackson.databind.*;
  3. import com.fasterxml.jackson.databind.node.ArrayNode;
  4. import com.fasterxml.jackson.databind.node.ObjectNode;
  5. import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
  6. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  7.  
  8. import java.io.IOException;
  9.  
  10. /**
  11. * Helper functions to handle JsonNode values.
  12. */
  13. public class Json {
  14. private static final ObjectMapper defaultObjectMapper = newDefaultMapper();
  15. private static volatile ObjectMapper objectMapper = null;
  16.  
  17. public static ObjectMapper newDefaultMapper() {
  18. ObjectMapper mapper = new ObjectMapper();
  19. mapper.registerModule(new Jdk8Module());
  20. mapper.registerModule(new JavaTimeModule());
  21. mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  22. return mapper;
  23. }
  24.  
  25. /**
  26. * Get the ObjectMapper used to serialize and deserialize objects to and from JSON values.
  27. * <p>
  28. * This can be set to a custom implementation using Json.setObjectMapper.
  29. *
  30. * @return the ObjectMapper currently being used
  31. */
  32. public static ObjectMapper mapper() {
  33. if (objectMapper == null) {
  34. return defaultObjectMapper;
  35. } else {
  36. return objectMapper;
  37. }
  38. }
  39.  
  40. private static String generateJson(Object o, boolean prettyPrint, boolean escapeNonASCII) {
  41. try {
  42. ObjectWriter writer = mapper().writer();
  43. if (prettyPrint) {
  44. writer = writer.with(SerializationFeature.INDENT_OUTPUT);
  45. }
  46. if (escapeNonASCII) {
  47. writer = writer.with(Feature.ESCAPE_NON_ASCII);
  48. }
  49. return writer.writeValueAsString(o);
  50. } catch (IOException e) {
  51. throw new RuntimeException(e);
  52. }
  53. }
  54.  
  55. /**
  56. * Convert an object to JsonNode.
  57. *
  58. * @param data Value to convert in Json.
  59. */
  60. public static JsonNode toJson(final Object data) {
  61. try {
  62. return mapper().valueToTree(data);
  63. } catch (Exception e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67.  
  68. /**
  69. * Convert a JsonNode to a Java value
  70. *
  71. * @param json Json value to convert.
  72. * @param clazz Expected Java value type.
  73. */
  74. public static <A> A fromJson(JsonNode json, Class<A> clazz) {
  75. try {
  76. return mapper().treeToValue(json, clazz);
  77. } catch (Exception e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81.  
  82. /**
  83. * Creates a new empty ObjectNode.
  84. */
  85. public static ObjectNode newObject() {
  86. return mapper().createObjectNode();
  87. }
  88.  
  89. /**
  90. * Creates a new empty ArrayNode.
  91. */
  92. public static ArrayNode newArray() {
  93. return mapper().createArrayNode();
  94. }
  95.  
  96. /**
  97. * Creates a new ArrayNode from scala.collection.Iterable<T extends JsonNode> nodes
  98. */
  99. public static <T extends JsonNode> ArrayNode newArray(scala.collection.Iterable<T> nodes) {
  100. return mapper().createArrayNode().addAll(scala.collection.JavaConversions.asJavaCollection(nodes));
  101. }
  102.  
  103.  
  104. /**
  105. * Convert a JsonNode to its string representation.
  106. */
  107. public static String stringify(JsonNode json) {
  108. return generateJson(json, false, false);
  109. }
  110.  
  111. /**
  112. * Convert a JsonNode to its string representation, escaping non-ascii characters.
  113. */
  114. public static String asciiStringify(JsonNode json) {
  115. return generateJson(json, false, true);
  116. }
  117.  
  118. /**
  119. * Convert a JsonNode to its string representation.
  120. */
  121. public static String prettyPrint(JsonNode json) {
  122. return generateJson(json, true, false);
  123. }
  124.  
  125. /**
  126. * Parse a String representing a json, and return it as a JsonNode.
  127. */
  128. public static JsonNode parse(String src) {
  129. try {
  130. return mapper().readTree(src);
  131. } catch (Throwable t) {
  132. throw new RuntimeException(t);
  133. }
  134. }
  135.  
  136. /**
  137. * Parse a InputStream representing a json, and return it as a JsonNode.
  138. */
  139. public static JsonNode parse(java.io.InputStream src) {
  140. try {
  141. return mapper().readTree(src);
  142. } catch (Throwable t) {
  143. throw new RuntimeException(t);
  144. }
  145. }
  146.  
  147. /**
  148. * Parse a byte array representing a json, and return it as a JsonNode.
  149. */
  150. public static JsonNode parse(byte[] src) {
  151. try {
  152. return mapper().readTree(src);
  153. } catch (Throwable t) {
  154. throw new RuntimeException(t);
  155. }
  156. }
  157.  
  158. /**
  159. * Inject the object mapper to use.
  160. * <p>
  161. * This is intended to be used when Play starts up. By default, Play will inject its own object mapper here,
  162. * but this mapper can be overridden either by a custom module.
  163. */
  164. public static void setObjectMapper(ObjectMapper mapper) {
  165. objectMapper = mapper;
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement