@@ -12,8 +12,12 @@ import 'json_key.dart';
1212
1313part 'json_serializable.g.dart' ;
1414
15+ // TODO: Remove typedef
16+ @Deprecated ('Use RenameType instead' )
17+ typedef FieldRename = RenameType ;
18+
1519/// Values for the automatic field renaming behavior for [JsonSerializable] .
16- enum FieldRename {
20+ enum RenameType {
1721 /// Use the field name without changes.
1822 none,
1923
@@ -35,7 +39,7 @@ enum FieldRename {
3539@JsonSerializable (
3640 checked: true ,
3741 disallowUnrecognizedKeys: true ,
38- fieldRename: FieldRename .snake,
42+ fieldRename: RenameType .snake,
3943)
4044@Target ({TargetKind .classType})
4145class JsonSerializable {
@@ -153,14 +157,14 @@ class JsonSerializable {
153157 /// Defines the automatic naming strategy when converting class field names
154158 /// into JSON map keys.
155159 ///
156- /// With a value [FieldRename .none] (the default), the name of the field is
160+ /// With a value [RenameType .none] (the default), the name of the field is
157161 /// used without modification.
158162 ///
159- /// See [FieldRename ] for details on the other options.
163+ /// See [RenameType ] for details on the other options.
160164 ///
161165 /// Note: the value for [JsonKey.name] takes precedence over this option for
162166 /// fields annotated with [JsonKey] .
163- final FieldRename ? fieldRename;
167+ final RenameType ? fieldRename;
164168
165169 /// When `true` on classes with type parameters (generic types), extra
166170 /// "helper" parameters will be generated for `fromJson` and/or `toJson` to
@@ -224,6 +228,20 @@ class JsonSerializable {
224228 /// `includeIfNull` , that value takes precedent.
225229 final bool ? includeIfNull;
226230
231+ /// The discriminator key used to identify the union type.
232+ ///
233+ /// Defaults to `type` .
234+ final String ? unionDiscriminator;
235+
236+ /// Defines the automatic naming strategy when converting class names
237+ /// to union type names.
238+ ///
239+ /// With a value [RenameType.none] (the default), the name of the class is
240+ /// used without modification.
241+ ///
242+ /// See [RenameType] for details on the other options.
243+ final RenameType ? unionRename;
244+
227245 /// A list of [JsonConverter] to apply to this class.
228246 ///
229247 /// Writing:
@@ -276,6 +294,8 @@ class JsonSerializable {
276294 this .converters,
277295 this .genericArgumentFactories,
278296 this .createPerFieldToJson,
297+ this .unionDiscriminator,
298+ this .unionRename,
279299 });
280300
281301 factory JsonSerializable .fromJson (Map <String , dynamic > json) =>
@@ -292,10 +312,12 @@ class JsonSerializable {
292312 createToJson: true ,
293313 disallowUnrecognizedKeys: false ,
294314 explicitToJson: false ,
295- fieldRename: FieldRename .none,
315+ fieldRename: RenameType .none,
296316 ignoreUnannotated: false ,
297317 includeIfNull: true ,
298318 genericArgumentFactories: false ,
319+ unionDiscriminator: 'type' ,
320+ unionRename: RenameType .none,
299321 );
300322
301323 /// Returns a new [JsonSerializable] instance with fields equal to the
@@ -318,6 +340,8 @@ class JsonSerializable {
318340 includeIfNull: includeIfNull ?? defaults.includeIfNull,
319341 genericArgumentFactories:
320342 genericArgumentFactories ?? defaults.genericArgumentFactories,
343+ unionDiscriminator: unionDiscriminator ?? defaults.unionDiscriminator,
344+ unionRename: unionRename ?? defaults.unionRename,
321345 );
322346
323347 Map <String , dynamic > toJson () => _$JsonSerializableToJson (this );
0 commit comments