json library
JSON for Dart objects: ObjectMapper turns objects into JSON and back,
without hand-written fromJson / toJson methods.
Import it to read and write JSON in your own code, to shape the JSON of
a model with Jackson-style annotations (@JsonProperty, @JsonIgnore,
@JsonInclude, @JsonFormat, @JsonTypeInfo, ...), or to add a
serializer for a type the mapper does not know. It is also exported by
package:spring_core_dart/spring_core_dart.dart. The web and Feign
starters use the same mapper for request and response bodies, so these
annotations shape the HTTP JSON too. The mapper reads metadata that
spring_core_dart_generator writes for classes marked
@Introspected(); there is no reflection. It also offers a tree model
(JsonNode), naming strategies and JsonMapper.builder().
@Introspected()
class Book {
const Book({required this.isbn, required this.title});
final String isbn;
final String title;
}
// In Spring Boot the mapper is a bean: inject ObjectMapper.
final json = mapper.writeValueAsString(
const Book(isbn: '1', title: 'Dune'),
);
final book = mapper.readValue<Book>(json);
Spring (Java): Jackson's com.fasterxml.jackson.databind, configured by
Spring Boot's JacksonAutoConfiguration.
Classes
- ArrayNode
- A JSON array: an ordered list of child nodes, which can be changed in place.
- BooleanNode
-
A JSON
trueorfalse. -
ClassType<
T> -
The TypeInfo of a class or scalar type that needs no further
description:
ClassType<String>(),ClassType<Book>(). - DynamicType
-
The TypeInfo of
dynamic,Object?or a type that is not described further: values are taken as they are.void(e.g. ofResponseEntity<void>) is DynamicType.forVoid, so a reader can ignore the value. -
EnumType<
E extends Enum> -
The TypeInfo of an enum, with its values, so a value can be found by
name:
EnumType<Mood>(Mood.values). jsonValue reads the field marked@JsonValue(), when the enum has one. -
GenericType<
T> -
The TypeInfo of a generic class other than the collections, with the
descriptions of its type arguments:
GenericType<ResponseEntity<Book>>([ClassType<Book>()]).Tis the whole type; arguments describe its type arguments in order. - JsonAlias
- Other JSON names a property (or constructor parameter) is read from, besides its own.
- JsonAnyGetter
- Writes the entries of a map property as extra properties of the object, after the declared ones.
- JsonAnySetter
- Collects the JSON properties a class does not have, instead of skipping them or failing.
- JsonCreator
- Marks the constructor (or factory constructor) the mapper creates objects with when reading JSON.
-
JsonDeserializer<
T extends Object> -
Custom code that builds values of
Tfrom decoded JSON, for a type the mapper cannot read on its own or should read differently. - JsonFeature
- An on/off setting of a mapper, named after Jackson's feature flags.
- JsonFormat
- Changes the JSON form of one property's value: a date pattern and time zone, or a shape (a number as a string, an enum by index, ...).
- JsonIgnore
- Leaves a property out of JSON in both directions.
- JsonIgnoreProperties
- Lists JSON properties a class ignores, and can make it accept unknown ones when reading.
- JsonInclude
-
Decides which values of a property, or of every property of a class,
are written: for example, leave out
nulls. - JsonMapper
- An ObjectMapper with a builder, for configuring a mapper step by step in Jackson 3's style.
- JsonMapperBuilder
- Collects settings, serializers and modules, then creates a JsonMapper with build.
- JsonModule
- A group of serializers and deserializers that is added to a mapper in one step.
- JsonNaming
- Sets the naming strategy of one class's properties, overriding the mapper's.
- JsonNode
-
One value of a JSON document held as a tree: an object, array, string,
number, boolean or
null, with typed accessors for navigating it. - JsonProperty
- Gives a property, constructor parameter or enum constant another name in JSON, or limits it to one direction.
- JsonPropertyOrder
- Puts chosen properties of a class first when it is written, for JSON that people read or that a fixed format expects.
-
JsonSerializer<
T extends Object> -
Custom code that writes values of
Tas JSON, for a type the mapper cannot handle or should write differently. - JsonSubType
-
One entry of JsonSubTypes: a subclass and the type ids it is written
and read with (Jackson's
@JsonSubTypes.Type). - JsonSubTypes
-
Lists the subclasses of a
@JsonTypeInfoclass and their type ids. - JsonTypeInfo
- Makes a class hierarchy polymorphic in JSON: each object carries a type id, so a value of the base type is read back as the right subclass.
- JsonTypeName
-
Sets the type id of a subclass in a
@JsonTypeInfohierarchy. - JsonUnwrapped
- Writes the properties of a nested object inline, in the enclosing object, instead of under its own key.
- JsonValue
- Makes one value stand for the whole object in JSON: an enum constant's JSON form, or a small value class written as a plain string or number.
- JsonView
- Puts a property into one or more views, so the same class can be written with fewer or more properties depending on who reads it.
-
ListType<
E> -
The TypeInfo of a
List<E>, with the description of its elements:ListType<Book>(ClassType<Book>()). -
MapType<
K, V> -
The TypeInfo of a
Map<K, V>, with the descriptions of its keys and values:MapType<String, int>(ClassType<String>(), ClassType<int>()). - MissingNode
-
The stand-in that JsonNode.path and JsonNode.at return where the
document has no node, so navigation can go on without
nullchecks. - NullNode
-
A JSON
nullvalue, which is present (unlike a MissingNode). - NumericNode
-
A JSON number: an
intwhen the JSON had no fraction, else adouble. - ObjectMapper
- Converts Dart objects to JSON and back: the one place where models, JSON text, decoded maps and JsonNode trees meet.
- ObjectNode
- A JSON object: named child nodes, kept in insertion order.
- ObjectReader
-
A reader bound to one
@JsonView: it reads only the properties of that view and skips the others, with its mapper's settings. - ObjectWriter
-
A writer bound to one
@JsonView: it writes only the properties of that view, with its mapper's settings. -
SetType<
E> -
The TypeInfo of a
Set<E>, with the description of its elements:SetType<String>(ClassType<String>()). - StringNode
-
A JSON string. asInt, asDouble and asBoolean convert numeric and
"true"/"false"text. -
TypeInfo<
T> -
A description of a full Dart type, such as
List<Book>orString?, that code can take apart at run time without reflection. -
TypeInfoVisitor<
R> - One method per kind of TypeInfo, called by TypeInfo.accept with the kind's type arguments in scope.
Enums
- DateTimeFeature
-
On/off settings for writing and reading
DateTimeandDuration, named after Jackson 3'sDateTimeFeature(set byspring.json.datatype.datetime.*; Spring Boot 4:spring.jackson.datatype.datetime.*). - DeserializationFeature
-
On/off settings for reading JSON, named after Jackson's
DeserializationFeature; the defaults are Spring Boot's. - JsonFormatShape
-
The JSON shapes JsonFormat.shape can ask for (Jackson's
JsonFormat.Shape). - JsonInclusion
-
Which property values are written, for JsonInclude and the mapper's
inclusion(Jackson'sJsonInclude.Include). - JsonPropertyAccess
-
The directions a property takes part in, for JsonProperty.access
(Jackson's
JsonProperty.Access). - JsonTypeId
-
What identifies the subclass in JSON, for JsonTypeInfo.use
(Jackson's
JsonTypeInfo.Id). - JsonTypeInclusion
-
Where the type id is placed, for JsonTypeInfo.include (Jackson's
JsonTypeInfo.As). - MapperFeature
-
On/off settings that change how the mapper sees classes, named after
Jackson's
MapperFeature. - PropertyNamingStrategy
-
A rule that turns Dart property names (
publishedAt) into JSON names (published_at), applied to every property of a class or of the whole mapper. - SerializationFeature
-
On/off settings for writing JSON, named after Jackson's
SerializationFeature.
Exceptions / Errors
- JsonMappingException
- The error the JSON mapper throws when JSON cannot be turned into the requested type, or a value cannot be written as JSON.
- JsonNodeException
-
The error a JsonNode accessor throws when the node has the wrong
kind, such as
stringValue()on a number, or cannot be converted.