JsonMapper class

An ObjectMapper with a builder, for configuring a mapper step by step in Jackson 3's style.

It behaves exactly like an ObjectMapper; the difference is the way in. JsonMapper.builder starts from the defaults, and ObjectMapper.rebuild() from an existing mapper's settings, so you can derive a variant (stricter reading, other naming) without changing the original, which stays immutable. Use it when you need a mapper besides the application's, or code ported from Jackson. In Spring Boot, inject the auto-configured ObjectMapper instead.

// A snake_case mapper for one external API, and a variant of it that
// keeps declaration order.
final mapper = JsonMapper.builder()
    .propertyNamingStrategy(PropertyNamingStrategy.snakeCase)
    .enable(DeserializationFeature.failOnUnknownProperties)
    .addModule(moneyModule)
    .build();
final declarationOrder = mapper
    .rebuild()
    .disable(MapperFeature.sortPropertiesAlphabetically)
    .build();

Spring (Java):

JsonMapper mapper = JsonMapper.builder()
    .propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
    .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
    .addModule(moneyModule)
    .build();
Inheritance

Constructors

JsonMapper({BeanIntrospector? introspector, PropertyNamingStrategy namingStrategy = PropertyNamingStrategy.lowerCamelCase, JsonInclusion inclusion = JsonInclusion.always, bool failOnUnknownProperties = false, bool acceptFloatAsInt = true, bool failOnNullForPrimitives = true, bool writeDatesAsTimestamps = false, bool writeDurationsAsTimestamps = false, bool writeDateTimestampsAsNanoseconds = true, bool readDateTimestampsAsNanoseconds = true, String? timeZone, bool indentOutput = false, bool sortPropertiesAlphabetically = true, bool defaultViewInclusion = false, Iterable<JsonSerializer<Object>> serializers = const [], Iterable<JsonDeserializer<Object>> deserializers = const [], Iterable<JsonModule> modules = const []})
A mapper with the given settings; each parameter is described on ObjectMapper.new.

Properties

acceptFloatAsInt → bool
Whether a number with a fraction read into an int is truncated (Jackson's ACCEPT_FLOAT_AS_INT); otherwise it is an error.
finalinherited
defaultViewInclusion → bool
Whether properties without @JsonView are included when a view is active (Jackson's DEFAULT_VIEW_INCLUSION).
finalinherited
deserializers → List<JsonDeserializer<Object>>
The custom deserializers, including those of the modules, tried in order before the built-in conversions.
finalinherited
failOnNullForPrimitives → bool
Whether a JSON null (or a missing constructor parameter) for a non-nullable int, double, num or bool is an error (Jackson's FAIL_ON_NULL_FOR_PRIMITIVES, on by default since Jackson 3); otherwise it is read as 0 / false.
finalinherited
failOnUnknownProperties → bool
Whether a JSON property a class does not have is an error rather than skipped; @JsonIgnoreProperties(ignoreUnknown: true) and @JsonAnySetter override it per class.
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
inclusion → JsonInclusion
Which property values are written, for properties and classes without @JsonInclude.
finalinherited
indentOutput → bool
Whether writeValueAsString indents its output for people to read.
finalinherited
introspector → BeanIntrospector
Where class metadata comes from: the generated introspections of the @Introspected classes.
finalinherited
namingStrategy → PropertyNamingStrategy
How Dart property names become JSON names, for classes without @JsonNaming and properties without a @JsonProperty name.
finalinherited
readDateTimestampsAsNanoseconds → bool
Whether integer timestamps are read as seconds (Jackson 3's READ_DATE_TIMESTAMPS_AS_NANOSECONDS), not milliseconds.
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
serializers → List<JsonSerializer<Object>>
The custom serializers, including those of the modules, tried in order before the built-in conversions.
finalinherited
sortPropertiesAlphabetically → bool
Whether the properties of an object are written sorted by JSON name (constructor parameters first), instead of in declaration order; spring.json.mapper.sort-properties-alphabetically.
finalinherited
timeZone → String?
The time zone dates are written in and dates without an offset are read in (Jackson's setTimeZone, spring.json.time-zone): UTC, Z or a fixed offset; null writes each DateTime in its own zone (local or UTC) and reads in UTC.
finalinherited
writeDatesAsTimestamps → bool
Whether DateTime is written as a number instead of ISO-8601 text.
finalinherited
writeDateTimestampsAsNanoseconds → bool
Whether timestamps are written as seconds with the fraction (Jackson 3's WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS), not milliseconds.
finalinherited
writeDurationsAsTimestamps → bool
Whether Duration is written as a number instead of ISO-8601 text.
finalinherited

Methods

convertValue<T>(Object? value, [TypeInfo<T>? type]) → T
Returns value converted to T by writing it as JSON and reading it back, like Jackson's convertValue.
inherited
createArrayNode() → ArrayNode
Returns a new empty ArrayNode, to build JSON by hand.
inherited
createObjectNode() → ObjectNode
Returns a new empty ObjectNode, to build JSON by hand.
inherited
isEnabled(JsonFeature feature) → bool
Whether feature is on in this mapper; a feature this mapper does not know gives its default.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readerWithView(Type view) → ObjectReader
Returns a reader that reads only the properties of view (see @JsonView); the other properties in the JSON are skipped, so a client cannot set them.
inherited
readList<E>(String json, [TypeInfo<E>? element]) → List<E>
Returns a List<E> read from the JSON array json; element describes a generic element type.
inherited
readMap<V>(String json, [TypeInfo<V>? value]) → Map<String, V>
Returns a Map<String, V> read from the JSON object json; value describes a generic value type.
inherited
readTree(String json) → JsonNode
Returns the JSON text json as a tree of JsonNodes, to read parts of a document without a class (Jackson's readTree). Throws a JsonMappingException for text that is not JSON.
inherited
readValue<T>(String json, [TypeInfo<T>? type]) → T
Returns a T read from the JSON text json.
inherited
rebuild() → JsonMapperBuilder
Returns a builder holding this mapper's settings, serializers and deserializers, to derive a variant; this mapper stays unchanged (Jackson 3's rebuild()).
inherited
toString() → String
A string representation of this object.
inherited
treeToValue<T>(Object? tree, [TypeInfo<T>? type]) → T
Returns a T read from decoded JSON tree (such as what an HTTP client or a message library already decoded) or from a JsonNode. type is as in readValue.
inherited
valueToTree(Object? value, [TypeInfo<Object?>? type]) → Object?
Returns value as decoded JSON: null, bools, numbers, strings, lists and maps, what jsonDecode returns. Use it to hand JSON to an API that takes maps, or to change it before encoding.
inherited
writerWithView(Type view) → ObjectWriter
Returns a writer that writes only the properties of view (see @JsonView), with this mapper's settings.
inherited
writeValueAsString(Object? value) → String
Returns value as JSON text, indented when indentOutput is on.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

builder() → JsonMapperBuilder
Returns a builder that starts from the default settings.