dart_json_annotations library
High-performance code generation for Dart/Flutter models.
Generates JSON serialization, copyWith, equatable, and union methods using a Rust-powered CLI for maximum speed.
Quick Start
import 'package:dart_json_annotations/dart_json_annotations.dart';
@Model() // JSON only (default)
class User {
final String name;
final int age;
User({required this.name, required this.age});
}
Model Presets
| Preset | Features | Lines/Model |
|---|---|---|
@Model.json() |
JSON only | ~25 |
@Model.data() |
JSON + copyWith + equatable | ~50 |
@Model.bloc() |
copyWith + equatable (no JSON) | ~35 |
@Model.mutable() |
copyWith (always) | ~40 |
Classes
- Assert
- Adds validation assertion for a field.
- Default
- Sets a default value for a field.
- GenericConfig
- Mutable class annotation (equivalent to Freezed's @unfreezed).
- Ignore
- Ignore this field from specific features.
-
Implements<
T> - Implements an interface for a union variant.
- JsonConverter
- Custom JSON converter for a field.
- JsonEnum
- Marks an enum for JSON serialization with custom value mapping.
- JsonKey
- Customizes JSON serialization for a specific field.
- JsonType
- Sets the naming convention for JSON field serialization.
- JsonValue
- Custom JSON value for enum variants.
- Model
- Unified model annotation with feature flags.
- ModelUnionValue
- Custom discriminator value for a union variant.
-
With<
T> - Mixes in a class for a union variant.
Enums
- JsonEnumValue
- NamingConvention
- Defines the naming convention for JSON serialization.