Jackson ObjectMapper: Difference between revisions
Line 44: | Line 44: | ||
==Serialization Features== | ==Serialization Features== | ||
======== | ====WRITE_DATES_AS_TIMESTAMPS==== | ||
* Feature that determines whether Date (and date/time) values | |||
* (and Date-based things like {@link java.util.Calendar}s) are to be | |||
* serialized as numeric timestamps (true; the default), | |||
* or as something else (usually textual representation). | |||
* If textual representation is used, the actual format is | |||
* one returned by a call to | |||
* {@link com.fasterxml.jackson.databind.SerializationConfig#getDateFormat}: | |||
* the default setting being {@link com.fasterxml.jackson.databind.util.StdDateFormat}, | |||
* which corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSZ" | |||
* (see {@link java.text.DateFormat} for details of format Strings). | |||
*<p> | |||
* Note: whether this feature affects handling of other date-related | |||
* types depend on handlers of those types, although ideally they | |||
* should use this feature | |||
*<p> | |||
* Note: whether {@link java.util.Map} keys are serialized as Strings | |||
* or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS}. | |||
*<p> | |||
* Feature is enabled by default, so that date/time are by default | |||
* serialized as timestamps. | |||
==Deserialization Features== | ==Deserialization Features== |
Revision as of 02:00, 14 November 2018
Internal
Overview
Thread Safety
ObjectMapper is thread safe, so it does not have to be created for each request that needs serialization/deserialization.
It is recommended to declare a static ObjectMapper instance that should be shared as much as possible.
The only problem with this approach is that the configuration of the mapper cannot be changed after it was shared, otherwise clients will rely on invalid assumptions,
With 2.0 and above, ObjectWriter and ObjectReader can be constructed by ObjectMapper. These objects are fully immutable, thread-safe and cheap to create?
Configuration
Mapper Features
Annotation Use
ObjectMapper can be configured to use or not annotation introspection. By default is true.
om.configure(MapperFeature.USE_ANNOTATIONS, true);
Use Getters as Setters
This feature determines whether the "getter" methods that handle Collections and Maps can be used for purpose of getting a reference to a Collection and Map to modify the property, without requiring a setter method. This is similar to how JAXB framework sets Collections and Maps. Note that such getters-as-setters methods have lower precedence than setters, so they are only used if no setter is found for the Map/Collection property. The feature is enabled by default.
om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, true);
Propagate Transient Marker
This feature determines how transient modifier for fields is handled: if disabled, it is only taken to mean exclusion of the field as accessor; if true, it is taken to imply removal of the whole property. Feature is disabled by default, meaning that existence of transient for a field does not cause Jackson to ignore getters or setters just ignoring the use of field for access.
om.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, false);
Serialization Features
WRITE_DATES_AS_TIMESTAMPS
* Feature that determines whether Date (and date/time) values * (and Date-based things like {@link java.util.Calendar}s) are to be * serialized as numeric timestamps (true; the default), * or as something else (usually textual representation). * If textual representation is used, the actual format is * one returned by a call to * {@link com.fasterxml.jackson.databind.SerializationConfig#getDateFormat}: * the default setting being {@link com.fasterxml.jackson.databind.util.StdDateFormat}, * which corresponds to format String of "yyyy-MM-dd'T'HH:mm:ss.SSSZ" * (see {@link java.text.DateFormat} for details of format Strings).
*
* Note: whether this feature affects handling of other date-related * types depend on handlers of those types, although ideally they * should use this feature *
* Note: whether {@link java.util.Map} keys are serialized as Strings * or not is controlled using {@link #WRITE_DATE_KEYS_AS_TIMESTAMPS}. *
* Feature is enabled by default, so that date/time are by default * serialized as timestamps.
Deserialization Features
Fail on Ignored Properties
This feature determines what happens when a property that has been explicitly marked as ignorable is encountered in input: if feature is enabled, JsonMappingException is thrown; if false, property is quietly skipped. The feature is disabled by default so that no exception is thrown. Another way to look at it is as configuration to ignore fields that appear in the JSON serialized format, but are not declared as members in the type. The same effect can be achieved by annotating the type with @JsonIgnoreProperties
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Specifying a Date Format
ISO 8601 date and time format can be automatically handled (converted from and to) by the ObjectMapper, as follows:
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new StdDateFormat());
More: