jsonツールクラスのパッケージと解析
1421 ワード
package com.chinatelecom.personalcustom.common.util;
import java.io.IOException;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
/**
* ,
*/
public static T jsonToEntity(String json, Class clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
//
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
return mapper.readValue(json, clazz);
}
/**
* JSON
*/
public static String entityToJson(Object entity){
return JSON.toJSONString(entity);
}
/**
* JsonNode,
*/
public static JsonNode jsonToJsonNode(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
//
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
//
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,true);
return mapper.readValue(json, JsonNode.class);
}
public static String objectToJson(Object object, Class cls)throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerSubtypes(cls);
String reqJson = mapper.writeValueAsString(object);
return reqJson;
}
}