JsonProperty class

Gives a property, constructor parameter or enum constant another name in JSON, or limits it to one direction.

Use it when the JSON name differs from the Dart one (published_at for publishedAt), for a single property where a naming strategy would be too broad. The name wins over the mapper's and the class's naming strategy. access makes a property one-way: a JsonPropertyAccess.readOnly property is written but ignored when read, a JsonPropertyAccess.writeOnly one (a password in a sign-up request) is read but never written.

There is no required flag: whether a property must be present comes from the Dart constructor. A parameter that is neither nullable nor has a default value must be in the JSON; a nullable one may be missing and reads as null.

// A REST model: snake_case date, password accepted but never echoed.
@Introspected()
class Book {
  const Book(this.isbn, this.publishedAt, {this.password});
  final String isbn; // must be present: not nullable, no default
  @JsonProperty('published_at')
  final DateTime publishedAt;
  @JsonProperty('', access: JsonPropertyAccess.writeOnly)
  final String? password;
}
// {"isbn": "978-0", "published_at": "2026-01-01T00:00:00.000Z"}

enum Color {
  @JsonProperty('RED')
  red,
  green, // "green"
}

Spring (Java):

@JsonProperty(required = true) String isbn;  // Dart: String isbn
@JsonProperty("published_at") Instant publishedAt;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) String password;
enum Color { @JsonProperty("RED") RED, GREEN }

Constructors

JsonProperty(String name, {JsonPropertyAccess access = JsonPropertyAccess.auto})
Names the property name in JSON; '' keeps its own name (after the naming strategy), for setting only access.
const

Properties

access → JsonPropertyAccess
In which directions the property is used; JsonPropertyAccess.auto by default.
final
hashCode → int
The hash code for this object.
no setterinherited
name → String
The JSON name; empty for the name the naming strategy gives.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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