-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Deserialization Features
cowtowncoder edited this page Apr 9, 2013
·
27 revisions
Jackson defines a set of features that relate to deserialization (reading JSON into Java Objects), and that can be changed on per-call basis, by using ObjectReader
.
You can also change defaults for ObjectMapper
, to be used as the base for all ObjectReader
instances.
Settings can be divided in couple of loose categories, as follows.
- 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 typejava.lang.Object
) or generic number type (java.lang.Number
); if disabled type will bejava.lang.Double
. - Does not affect explicit type (i.e. if expected result type is
java.lang.Double
or such)
- Controls whether
- 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, typejava.math.BigInteger}}} is used for binding to generic types; if disabled,
java.lang.Integeror
java.lang.Long` is used (smallest applicable type that can contain actual value)
- Similar to
- USE_JAVA_ARRAY_FOR_JSON_ARRAY (default: false)
- Determines whether to bind JSON Arrays as
java.util.Lists
orObject[]
instances, when binding to nominal type ofjava.lang.Object
: if disabled, asjava.util.List
, if enabled asObject[]
.
- Determines whether to bind JSON Arrays as
- READ_ENUMS_USING_TO_STRING (default: false)
- Determines which method is used to determine expected serialization of an Enum: if false (default), use
Enum.name()
; if true,Enum.toString()
.
- Determines which method is used to determine expected serialization of an Enum: if false (default), use
- ACCEPT_EMPTY_STRING_AS_NULL_OBJECT (default: false)
- 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.
- READ_UNKNOWN_ENUM_VALUES_AS_NULL (default: false)
- Feature that allows unknown
Enum
values to be parsed as Java null values. If disabled, unknown Enum values will throw exceptions. - Note that in some cases this will basically ignore unknown Enum values; this is the keys for keys of
java.util.EnumMap
and values ofjava.util.EnumSet
(because nulls are not accepted in these cases.
- Feature that allows unknown
- READ_DATE_TIMESTAMPS_AS_NANOSECONDS (default: false) -- Since Jackson 2.2
- Feature that controls whether numeric timestamp values are expected to be written using nanosecond timestamps (enabled) or not (disabled); if disabled, standard millisecond timestamps are assumed.
- FAIL_ON_UNKNOWN_PROPERTIES (default: true)
- 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 aJsonMappingException
(when enabled), or just quietly ignored (when disabled)
- 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
- FAIL_ON_NULL_FOR_PRIMITIVES (default: false)
- Determines whether JSON null is acceptable for Java primitive types (
int
,boolean
,double
etc); if set to 'false', default value is used; if 'true', aJsonProcessingException
will be thrown.
- Determines whether JSON null is acceptable for Java primitive types (
- FAIL_ON_NUMBERS_FOR_ENUMS (default: false)
- 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
- 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
- FAIL_ON_INVALID_SUBTYPE (default: true) (since Jackson 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).
- Determines what happens when type information for polymorphic types (see
- ACCEPT_SINGLE_VALUE_AS_ARRAY (default: false)
- 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.
- UNWRAP_ROOT_VALUE (default: false)
- Feature to allow "unwrapping" root-level JSON value, to match setting of
SerializationFeature#WRAP_ROOT_VALUE
used for serialization. - Will verify that the root JSON value is a JSON Object, and that it has a single property with expected root name. If not, a
JsonMappingException
is thrown; otherwise value of the wrapped property will be deserialized as if it was the root value.
- Feature to allow "unwrapping" root-level JSON value, to match setting of