cn.t.util.common.JsonUtil Maven / Gradle / Ivy
package cn.t.util.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class JsonUtil {
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
public static T deserialize(String data, Class clazz) throws IOException {
return objectMapper.readValue(data, clazz);
}
public static T deserialize(String data, TypeReference tTypeReference) throws IOException {
return objectMapper.readValue(data, tTypeReference);
}
public static String serialize(Object obj) throws JsonProcessingException {
return objectMapper.writeValueAsString(obj);
}
}