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
-
- Object
- ObjectMapper
- JsonMapper
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< serializers = const [], Iterable<Object> >JsonDeserializer< deserializers = const [], Iterable<Object> >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
intis truncated (Jackson'sACCEPT_FLOAT_AS_INT); otherwise it is an error.finalinherited - defaultViewInclusion → bool
-
Whether properties without
@JsonVieware included when a view is active (Jackson'sDEFAULT_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-nullableint,double,numorboolis an error (Jackson'sFAIL_ON_NULL_FOR_PRIMITIVES, on by default since Jackson 3); otherwise it is read as0/false.finalinherited - failOnUnknownProperties → bool
-
Whether a JSON property a class does not have is an error rather than
skipped;
@JsonIgnoreProperties(ignoreUnknown: true)and@JsonAnySetteroverride 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
@Introspectedclasses.finalinherited - namingStrategy → PropertyNamingStrategy
-
How Dart property names become JSON names, for classes without
@JsonNamingand properties without a@JsonPropertyname.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,Zor a fixed offset;nullwrites eachDateTimein its own zone (local or UTC) and reads in UTC.finalinherited - writeDatesAsTimestamps → bool
-
Whether
DateTimeis 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
Durationis written as a number instead of ISO-8601 text.finalinherited
Methods
-
convertValue<
T> (Object? value, [TypeInfo< T> ? type]) → T -
Returns
valueconverted toTby writing it as JSON and reading it back, like Jackson'sconvertValue.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
featureis 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 arrayjson;elementdescribes a generic element type.inherited -
readMap<
V> (String json, [TypeInfo< V> ? value]) → Map<String, V> -
Returns a
Map<String, V>read from the JSON objectjson;valuedescribes a generic value type.inherited -
readTree(
String json) → JsonNode -
Returns the JSON text
jsonas a tree of JsonNodes, to read parts of a document without a class (Jackson'sreadTree). Throws a JsonMappingException for text that is not JSON.inherited -
readValue<
T> (String json, [TypeInfo< T> ? type]) → T -
Returns a
Tread from the JSON textjson.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
Tread from decoded JSONtree(such as what an HTTP client or a message library already decoded) or from a JsonNode.typeis as in readValue.inherited -
valueToTree(
Object? value, [TypeInfo< Object?> ? type]) → Object? -
Returns
valueas decoded JSON:null, bools, numbers, strings, lists and maps, whatjsonDecodereturns. 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
valueas 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.