JsonSchemaGenerator class final

Writes the JSON Schema (draft 2020-12) of @Introspected classes from their introspection, without reflection.

Use it to publish the shape of your JSON: an API's request bodies, a configuration file, a message format. The schema describes the JSON ObjectMapper writes and reads with its default settings and the given namingStrategy: JSON names (@JsonProperty, @JsonNaming), left-out properties (@JsonIgnore, @JsonIgnoreProperties, @JsonAnyGetter / @JsonAnySetter), @JsonProperty(access:) as readOnly / writeOnly, @JsonFormat shapes, and the validation constraints as keywords.

What a type becomes:

  • String → string; int → integer; double, num → number; bool → boolean;
  • DateTime → string with format date-time; Duration → string with format duration; Uri → string with format uri-reference; BigInt → a string of digits or an integer;
  • an enum → its JSON values (@JsonValue too) in enum;
  • List → array with items; Set → the same, uniqueItems; Map → object with additionalProperties;
  • an introspected class → a $ref to #/$defs/<ClassName> (the root class is #, so cycles work);
  • a nullable type also allows null; dynamic, Object and JsonNode allow anything.

required lists the constructor parameters the mapper needs (required, not nullable, not read-only) and the properties marked @NotNull. Constraints become keywords whatever their groups: @NotEmpty, @NotBlank (also the pattern \S), @Size, @Min, @Max, @DecimalMin, @DecimalMax, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @Matches (anchored, without its flags), @Email and @Each (for the elements).

Not described: polymorphism (@JsonTypeInfo), @JsonUnwrapped properties, classes with their own fromJson / toJson (any JSON), and the mapper settings that write dates and durations as numbers.

final schema = JsonSchemaGenerator().schemaOf(Order);
// {$schema: https://json-schema.org/draft/2020-12/schema, title: Order,
//  type: object, properties: {...}, required: [...], $defs: {...}}

Spring (Java): Spring has no counterpart; Java applications use Jackson's jsonSchema module or victools' jsonschema-generator.

Constructors

JsonSchemaGenerator({Introspector? introspector, PropertyNamingStrategy namingStrategy = PropertyNamingStrategy.lowerCamelCase})
A generator reading classes from introspector (Introspector.shared by default) and naming properties with namingStrategy, as the ObjectMapper it describes does.

Properties

hashCode → int
The hash code for this object.
no setterinherited
introspector → Introspector
Where the classes are described.
final
namingStrategy → PropertyNamingStrategy
How Dart property names become JSON names, for classes without @JsonNaming.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

all() → Map<String, Map<String, Object?>>
The schemas of every class of the introspector, by class name; enums and generic classes are left out, and of two classes with the same name the first.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
schemaOf(Type type) → Map<String, Object?>
The schema of the introspected class type, the classes it uses in its $defs. Throws an ArgumentError when type is not introspected.
schemasOf(Iterable<Type> types) → Map<String, Map<String, Object?>>
The schemas of types, by class name.
toString() → String
A string representation of this object.
inherited

Operators

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

Constants

draft → const String
The $schema of every generated schema: draft 2020-12.