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→stringwith formatdate-time;Duration→stringwith formatduration;Uri→stringwith formaturi-reference;BigInt→ a string of digits or an integer;- an enum → its JSON values (
@JsonValuetoo) inenum; List→arraywithitems;Set→ the same,uniqueItems;Map→objectwithadditionalProperties;- an introspected class → a
$refto#/$defs/<ClassName>(the root class is#, so cycles work); - a nullable type also allows
null;dynamic,ObjectandJsonNodeallow 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 withnamingStrategy, as theObjectMapperit 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 whentypeis 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