spring Jackson 配置笔记

配置代码

        // 设置输出时包含属性的风格
        this.findAndRegisterModules();
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL)

        // 允许单引号、允许不带引号的字段名称
        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)

        this.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        this.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        this.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        this.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        this.setDateFormat(SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


        // 设置输入时忽略在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")

config 常用配置枚举

SerializationFeature

  • WRITE_DATES_AS_TIMESTAMPS
  • WRITE_DATE_KEYS_AS_TIMESTAMPS
  • WRITE_DATES_WITH_ZONE_ID
  • WRITE_DURATIONS_AS_TIMESTAMPS
  • WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS
  • WRITE_ENUMS_USING_TO_STRING
  • WRITE_ENUMS_USING_INDEX
  • WRITE_NULL_MAP_VALUES
  • WRITE_EMPTY_JSON_ARRAYS
  • WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED
  • WRITE_BIGDECIMAL_AS_PLAIN
  • WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS

MapperFeature

  • USE_STATIC_TYPING
  • USE_STD_BEAN_NAMING

DeserializationFeature

  • USE_BIG_DECIMAL_FOR_FLOATS
  • USE_BIG_INTEGER_FOR_INTS
  • USE_LONG_FOR_INTS
  • ACCEPT_SINGLE_VALUE_AS_ARRAY
  • UNWRAP_SINGLE_VALUE_ARRAYS
  • ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
  • ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT
  • ACCEPT_FLOAT_AS_INT
  • READ_ENUMS_USING_TO_STRING
  • READ_UNKNOWN_ENUM_VALUES_AS_NULL
  • READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE
  • READ_DATE_TIMESTAMPS_AS_NANOSECONDS
  • ADJUST_DATES_TO_CONTEXT_TIME_ZONE

常用配置解释

  • setVisibility 可以设置Key的方式

  • 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 就是规范输出。

posted @ 2018-09-25 22:22  NewSea  阅读(2827)  评论(0编辑  收藏  举报