jackson gson
class JsonUtils {
private static final Gson GSON =
new GsonBuilder().disableHtmlEscaping().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
public static String toJson(Object obj) {
return GSON.toJson(obj)
}
}
public class JsonSerializer {
private static final ObjectMapper objectMapper = new ObjectMapper();
private JsonSerializer(){}
static {
objectMapper.configure(com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//对于值位null的则不进行序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));
}
public static String toJson(Object x) {
try {
return objectMapper.writeValueAsString(x);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <T> T fromJson(String json, Class<T> targetType) {
try {
return objectMapper.readValue(json, targetType);
相关推荐
pigsmall 2020-11-19
linuxprobe0 2013-04-01
83560193 2013-06-25
kidneybeans 2013-06-17
加菲猫园 2013-06-16
年轻就要对味 2014-07-11
稻草人的高粱地 2014-07-02
smilebestSun 2014-06-12
xusong 2014-05-17
zfszhangyuan 2013-09-12
80183053 2013-09-12
gaozhlzh 2013-09-11
87453169 2014-01-17
Tom天天 2013-07-12
有心就有方向 2012-09-03
zfyaixue 2013-06-14
Ladyseven 2020-07-25