ObjectMapper配置

JSON-to-Java mapping features

Simple On/off Features

Following on/off features are defined in DeserializationConfig.Feature (Jackson 1.x) or DeserializationFeature (Jackson 2.x), and include:

These can be grouped as follows:

Annotation processing
  • USE_ANNOTATIONS (default: true) (since 1.2)

    • Controls whether any annotation introspection is used for configuring data binding: if disabled, all annotations are ignored (independent of setting for AnnotationIntrospector in use)

    • note: since 1.8, disable this feature will try to remove ALL annotation processing; this to help on platforms like Android, where trying to access unavailable annotation types (JAXB annotations for value types) can result in exception, even if not specifically looking for that annotation type.

  • AUTO_DETECT_SETTERS(true)
    • Controls whether "setter" methods are automatically detected (as opposed to located by annotations); if enabled, non-private single-argument non-static methods are recognized as setters (method used to set value of the logical property). If disabled, only ones explicitly annotated (using @JsonProperty, for example) are recognized as deserializable properties

  • AUTO_DETECT_CREATORS (default: true)
    • Controls whether potential creator methods (public single-argument constructors, public static methods that return instance of class itself (aka factory methods)) can be automatically detected without being annotated with @JsonCreator (or similar for alternate annotation introspector).

  • AUTO_DETECT_FIELDS (default: true)
    • Similar to AUTO_DETECT_SETTERS, controls whether public instance fields are recognized as deserializable properties.

  • USE_GETTERS_AS_SETTERS (default: true)
    • Controls whether "getters" that return Collection or Map types can be used for "setting" values (same as how JAXB API works with XML), so that separate "setter" method is not needed.

    • Even if enabled, explicit "setter" method will have precedence over implicit getter-as-setter, if one exists.
  • CAN_OVERRIDE_ACCESS_MODIFIERS (default: true)
    • Whether non-public fields and methods can be forced accessible (using JDK provided methods or not): if disabled, non-public fields and methods can not be used for properties.
    • The main reason to disable this feature is if running on a security-restricted platform where trying to change access rights results in a runtime error.

Conversions
  • USE_BIG_DECIMAL_FOR_FLOATS (default: false)
    • Controls whether java.math.BigDecimal is used for deserializing JSON floating point numbers, when resulting type is a generic type (java.lang.Object) or generic number type (java.lang.Number); if disabled type will be java.lang.Double

    • Does not affect explicit type (i.e. if expected result type is java.lang.Double or such)

  • USE_BIG_INTEGER_FOR_INTS (default: false)
    • Similar to USE_BIG_DECIMAL_FOR_FLOATS, but used when value to map is a JSON integer number (numeric value without decimal point). If enabled, type java.math.BigInteger is used for binding to generic types; if disabled, java.lang.Integer or java.lang.Long is used (smallest applicable type that can contain actual value)

  • READ_ENUMS_USING_TO_STRING (default: false) (since 1.6)

    • Determines which method is used to determine expected serialization of an Enum: if false (default), use Enum.name(); if true, Enum.toString().

  • ACCEPT_EMPTY_STRING_AS_NULL_OBJECT (default: false) (since 1.8)

    • Determines whether empty JSON String value is accepted as null value for regular POJOs ("beans") with data-binding: this can be useful when dealing endpoints written in a language that has loose typing and may represent missing objects as Empty Strings.
  • ACCEPT_SINGLE_VALUE_AS_ARRAY (default: false) (since 1.8)

    • Allows auto-conversion from non-JSON-array values to single-element arrays and Collections (adding implicit "array wrapper"): this is sometimes necessary for interoperability, as some libraries and frameworks omit JSON arrays when serializing single-element arrays.
  • USE_JAVA_ARRAY_FOR_JSON_ARRAY (default: false) (since 1.9)

    • Determines whether to bind JSON Arrays as java.util.Lists or Object[] instances, when binding to nominal type of java.lang.Object: if disabled, as List, if enabled as Object[].

Failure handling
  • FAIL_ON_UNKNOWN_PROPERTIES (default: true) (since 1.2)

    • Used to control whether encountering of unknown properties (one for which there is no setter; and there is no fallback "any setter" method defined using @JsonAnySetter annotation) should result in a JsonMappingException (when enabled), or just quietly ignored (when disabled)

  • FAIL_ON_NULL_FOR_PRIMITIVES (default: false) (since 1.7)

    • Determines whether JSON null is acceptable for Java primitive types (int, boolean, double etc); if set to 'false', default value is used; if 'true', a JsonProcessingException will be thrown.

  • FAIL_ON_NUMBERS_FOR_ENUMS (default: false) (since 1.7)

    • Determines whether JSON integer numbers (0, 1, 2, ...) can be deserialized into Java Enum types; if set to 'false', this is legal and value is used to deserialize value that matches Enum.ordinal(); if 'true', trying to deserialize JSON number into Enum throws a JsonProcessingException

  • FAIL_ON_INVALID_SUBTYPE (default: true) (since [[JacksonRelease22 | 2.2])

    • Determines what happens when type information for polymorphic types (see @JsonTypeInfo) is either missing or invalid (unmappable); if enabled, exception is thrown; if disabled, null reference is used instead (as type can not be determined).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值