Skip to main content

Defaults and Converters

This page reflects complex_example.dart, initialization_params_example.dart, and test_json_key.dart.

Default values with JsonKey

(generateJson: true)
abstract class $TreeNode {
(defaultValue: "root")
String get value;

(defaultValue: const [])
List<$TreeNode>? get children;

(defaultValue: Duration(seconds: 5))
Duration get timeout;
}

Custom JSON converters

(generateJson: true, generateFilter: true)
abstract class $InitializationParamsExample {
Duration get timeout => Duration(seconds: 5);
bool get forceRefresh => true;

(toJson: LocaleConverter.toJson, fromJson: LocaleConverter.fromJson)
Locale? get locale;
}

The same converter pattern works with plain json_annotation classes as well.