配置代码
// 设置输出时包含属性的风格
this.findAndRegisterModules();
this.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
// 允许单引号、允许不带引号的字段名称
this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
this.configure(MapperFeature.USE_STD_BEAN_NAMING, true)
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
// 空值处理为空串
this.serializerProvider.setNullValueSerializer(object : JsonSerializer<Any>() {
@Throws(IOException::class, JsonProcessingException::class)
override fun serialize(value: Any, jgen: JsonGenerator,
provider: SerializerProvider) {
jgen.writeString("")
}
})
// 设置时区
this.setTimeZone(TimeZone.getDefault())//getTimeZone("GMT+8:00")
常用配置解释
MapperFeature.USE_STD_BEAN_NAMING 直接输出原始的字段名。
/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.5/jackson-databind-2.9.5-sources.jar!/com/fasterxml/jackson/databind/util/BeanUtil.java
/**
* @since 2.5
*/
public static String okNameForMutator(AnnotatedMethod am, String prefix,
boolean stdNaming) {
String name = am.getName();
if (name.startsWith(prefix)) {
return stdNaming
? stdManglePropertyName(name, prefix.length())
: legacyManglePropertyName(name, prefix.length());
}
return null;
}
stdManglePropertyName 就是原始输出。
legacyManglePropertyName 就是规范输出。