Jackson Json Serializer Exception

Posted on -
  1. Jackson Serializer Example

Document your code

Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code. Tennu le full song - jai veeru mp3 download.

Sign up for free See pricing for teams and enterprises

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; for example:

You can also change defaults for ObjectMapper, to be used as the base for all ObjectReader instances, using enable(feature), disable(feature) and configure(feature, state) methods.

Jackson JSON: deserialize a list of objects of subclasses of an abstract class Posted on March 5, 2015 by Davis Molinari In this post we see how to serialize and deserialize in JSON a Java class that declares an instance variable consisting in a list of objects of an abstract class that contains objects of its various concrete subclasses.

Settings can be divided in couple of loose categories, as follows.

Type 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)
  • USE_JAVA_ARRAY_FOR_JSON_ARRAY (default: false)
    • 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 java.util.List, if enabled as Object[].
  • 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().

Structural conversions

  • 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.
  • UNWRAP_SINGLE_VALUE_ARRAYS (default: false) (since 2.4)
    • Allows auto-conversion from single-element arrays to non-JSON-array values : this is similar to the ACCEPT_SINGLE_VALUE_AS_ARRAY feature but works in the opposite manner (i.e. if you have a bound property that is not an array or collection, a single value array in JSON would be acceptable to bind to that property). If the JSON value contains more than one element in the array, deserialization will still fail.

Value conversions, coercion

  • ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT (default: false) (since 2.5)
    • Determines whether empty Array value ('[ ]' in JSON) is accepted as null value for regular POJOs ('beans') with) with method defined using @JsonAnySetter annotation) should result in a JsonMappingException (when enabled), or just quietly ignored (when disabled)
  • FAIL_ON_INVALID_SUBTYPE (default: true) (since 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).
  • 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', a JsonProcessingException will be thrown.
  • 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
  • FAIL_ON_READING_DUP_TREE_KEY (default: false) (since 2.3)
    • Determines what happens when incoming JSON has duplicate property names, when parsing as JSON Trees (JsonNode): if enabled, an exception is thrown; if false, no exception thrown and the last value is used.
  • FAIL_ON_UNRESOLVED_OBJECT_IDS (default: true) (since 2.5)
    • Feature that determines what happens if an Object Id reference is encountered that does not refer to an actual Object with that id ('unresolved Object Id')
    • If enabled, a JsonMappingException will be thrown
    • If disabled, null reference is used in place of resolved Java Object
  • FAIL_ON_MISSING_CREATOR_PROPERTIES (default: false) (since 2.6)
    • Feature that determines what happens if one or more Creator properties (properties bound to parameters of Creator method (constructor or static factory method)) are missing value to bind to from content.
    • If enabled, a JsonMappingException (with information like name of the first missing property) will be thrown
    • If disabled, the 'null value' for matching type is used instead (null value is determined by JsonSerializer for the type, usually null but custom deserializers may choose other placeholder).
  • WRAP_EXCEPTIONS (default:true)
    • Determines whether Jackson is to catch Exceptions other than IOException and its own exception types, to add more information on location and path to property.
    • If disabled, exceptions like IllegalArgumentException are passed as-is; if enabled, they will be caught and re-thrown as a Jackson exception (JsonMappingException or its subtype)

Other

  • EAGER_DESERIALIZER_FETCH (default: true):
    • Determines whether ObjectReader should try to fetch JsonDeserializer to use when constructed and given type information to locate it.
    • The benefit of enabling this feature is that if ObjectReader is used more than once, lookup only occurs once; if disabled, lookup needs to be performed every time a deserialization (via readValue()) is called.
Clone this wiki locally

Document your code

Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.

Sign up for free See pricing for teams and enterprises

Jackson defines a set of features that relate to serialization (writing Java objects as JSON), and that can be changed on per-call basis (by using ObjectWriter). For example:

You can also change defaults for ObjectMapper, to be used as the base for all ObjectWriter instances, using enable(feature), disable(feature) and configure(feature, state) methods.

Exception

Settings can be divided in couple of loose categories, as follows.

Generic output features

  • WRAP_ROOT_VALUE (default: false)
    • When enabled, will wrap output in a single-property JSON Object. Name of wrapper property is based on one of:
      • @JsonRootName annotation value, or
      • @XmlRootElement if (and only if) using JAXB Annotations, or
      • Simple Class name (no package prefix) of the serialized instance (or value type if static typing used or root type specified), if no annotation is specified.
    • Note that this settings does NOT have effect when writing XML since XML ALWAYS requires separate root element (true for Jackson versions 2.3 and later)
  • INDENT_OUTPUT (default: false)
    • Controls whether output is indented using the default indentation ('pretty-printing') mechanism (2-space, platform linefeeds) or not.
    • NOTE: not all data formats support indentation: binary formats do not have the concept (they are not human-readable); and CSV has flat structure and can not be 'prettified'

Error handling

Jackson Json Serializer Exception
  • FAIL_ON_EMPTY_BEANS (default: true)
    • Controls whether exception is thrown if no serializer is found for a type (Class); and type has neither getters nor recognized annotations (ones configured AnnotationIntrospector recognizes, including JAXB Annotations if JAXB introspector used).
  • WRAP_EXCEPTIONS (default: true)
    • Feature that determines whether Jackson code should catch and wrap Exceptions (but never Errors) to add additional information about location (within input) of problem or not.
    • If enabled, most exceptions will be caught and re-thrown (exception specifically being thatjava.io.IOExceptions may be passed as-is, since they are declared as throwable); this can be convenient both in that all exceptions will be checked and declared, and so there is more contextual information. However, sometimes calling application may just want 'raw' unchecked exceptions passed as is.

Output life-cycle features

  • CLOSE_CLOSEABLE (default: false)
    • If enabled, ObjectMapper will call close() and root values that implement java.io.Closeable; including cases where exception is thrown and serialization does not completely succeed.
    • Can be useful for resource clean up; and specifically allows for building wrappers that can be useful when serializing referential values (like, say, serializing a {{{java.io.File}}} value by reading contents of a file and outputting as JSON String or base64-encoded binary)
  • FLUSH_AFTER_WRITE_VALUE (default: true)
    • Determines whether JsonGenerator.flush() is automatically called after ObjectMappers writeValue(JsonGenerator, ..) is called or not (note: does NOT affect methods that do not take JsonGenerator) -- if true flush() is called; if false, it is not called.
    • Main reason to disable this feature is performance; for network connections flushing may send message earlier than optimal, and with some compressing streams compression block may complete with flush().

Datatype-specific serialization

  • WRITE_DATES_AS_TIMESTAMPS (default: true)
    • Controls whether Date / Time values are to be written out as numeric (64-bit) timestamps (if true) or as Strings. If latter, format used is defined by SerializationConfig.getDateFormat
  • WRITE_DATE_KEYS_AS_TIMESTAMPS (default: false)
    • Feature that determines whether java.util.Dates (and sub-types) used as java.util.Map keys are serialized as timestamps or not. If not, will be serialized as textual (ISO-8601) values.
  • WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS (default: false)
    • Controls how basic char[] values are serialized: if enabled, they will be serialized as full JSON Arrays, with JSON Strings (of length 1 character) as elements; if disabled (which is the default) as a single JSON String consisting of all characters of the array.
  • WRITE_ENUMS_USING_TO_STRING (default: false)
    • Determines which method is used to determine expected serialization of an Enum (when serialized as String): if false (default), use Enum.name(); if true, Enum.toString().
  • WRITE_ENUMS_USING_INDEX (default: false)
    • Determines whether Enums are to be serialized as integers (true), or Strings (false): if integers, Enum.ordinal() value is used as serialization.
  • WRITE_NULL_MAP_VALUES (default: true)
    • Determines whether Map entries with value null are serialized or not.
  • WRITE_EMPTY_JSON_ARRAYS (default: true)
    • Allows suppressing serialization of empty Collections/arrays.
  • WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED (default: false)
    • Feature to change handling of single-element Arrays, java.util.Collections, so that such arrays/collections are serialized as 'unwrapped' elements, not as JSON Arrays.
  • WRITE_BIGDECIMAL_AS_PLAIN (default: false) (since Jackson 2.2)
    • If enabled, will prevent use of scientific notation (use of 'e' in value to normalize scale of mantisaa); if disabled, will use scientific notation when necessary.
  • WRITE_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.
  • ORDER_MAP_ENTRIES_BY_KEYS (default: false)
    • Feature that determines whether java.util.Map entries are first sorted by key before serialization or not: if enabled, additional sorting step is performed if necessary (not necessary for java.util.SortedMaps), if disabled, no additional sorting is needed.

Other

  • EAGER_SERIALIZER_FETCH (default: true) (since Jackson 2.1)
    • Feature that determines whether ObjectWriter should try to eagerly fetch necessary JsonSerializer when possible. This improves performance in cases where similarly configured ObjectWriter instance is used multiple times; and should not significantly affect single-use cases.
    • There should not be any need to normally disable this feature: only consider that if there are actual perceived problems.
  • USE_EQUALITY_FOR_OBJECT_ID (default: false) (since Jackson 2.3)
    • Feature that determines whether Object Identity is compared using true JVM-level identity of Object (false); or, equals() method.
    • Setting this to true (to use equals()) is sometimes useful when dealing with Database-bound objects with ORM libraries like Hibernate.

Jackson Serializer Example

Clone this wiki locally