Java tutorial
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.ocsoft.olivia.utils; import java.io.File; import java.io.IOException; import java.util.Map; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.ObjectNode; /** * JSON??. * @author tohhy */ public class JsonUtils { /** * mapper?. */ private static final ObjectMapper mapper = new ObjectMapper(); static { mapper.enable(SerializationFeature.INDENT_OUTPUT); } /** * * @param json * @return * @throws IOException */ public static JsonNode readTree(String json) throws IOException { return mapper.readTree(json); } /** * ObjectNode?????. * @return ????ObjectNode? */ public static ObjectNode createObjectNode() { return mapper.createObjectNode(); } /** * ??JSON?????. * @param value ?? * @return ??JSON * @throws JsonProcessingException JSON?????? */ public static String toJsonString(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value); } /** * * @param value * @return */ public static JsonNode toJsonNode(Object value) { return mapper.valueToTree(value); } /** * * @param file * @param object * @throws IOException */ public static void writeObjectToFile(File file, ObjectNode object) throws IOException { mapper.writeValue(file, object); } /** * JSON?????????. * @param value ?JSON * @param type ? * @return ?? * @throws IOException ?????????? */ public static <T> T readJsonString(String value, TypeReference<T> type) throws IOException { return mapper.readValue(value, type); } /** * * @param value * @param klass * @return * @throws IOException */ public static <T> T readJsonString(String value, Class<T> klass) throws IOException { return mapper.readValue(value, klass); } /** * * @param value * @return * @throws IOException */ public static Map<String, String> readJsonStringAsStringMap(String value) throws IOException { return mapper.readValue(value, new TypeReference<Map<String, String>>() { }); } /** * * @param value * @return * @throws IOException */ public static JsonNode readJsonStringAsNode(String value) throws IOException { return mapper.readTree(value); } /** * * @param node * @param type * @return * @throws IOException */ public static <T> T readJsonNode(JsonNode node, TypeReference<T> type) throws IOException { return readJsonParser(node.traverse(), type); } /** * * @param node * @param type * @return * @throws IOException */ public static <T> T readJsonNode(JsonNode node, Class<T> type) throws IOException { return readJsonParser(node.traverse(), type); } /** * * @param jsonp * @param type * @return * @throws IOException */ public static <T> T readJsonParser(JsonParser jsonp, TypeReference<T> type) throws IOException { return mapper.readValue(jsonp, type); } /** * * @param jsonp * @param type * @return * @throws IOException */ public static <T> T readJsonParser(JsonParser jsonp, Class<T> type) throws IOException { return mapper.readValue(jsonp, type); } }