cn.authing.sdk.java.util.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of authing-java-sdk Show documentation
Show all versions of authing-java-sdk Show documentation
java backend sdk for authing
package cn.authing.sdk.java.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* JsonUtils
*
* @author chho
*/
public class JsonUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
//属性为NULL不被序列化
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//反序列化的时候如果多了其他属性,不抛出异常
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//如果是空对象的时候,不抛异常
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
public static T deserialize(String content, Class valueType) {
try {
return OBJECT_MAPPER.readValue(content, valueType);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String serialize(Object value) {
try {
return OBJECT_MAPPER.writeValueAsString(value);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}