@@ -31,6 +31,26 @@ enum FieldRename {
3131 screamingSnake,
3232}
3333
34+ /// Values for the automatic class renaming behavior for [JsonSerializable]
35+ /// with sealed classes.
36+ enum UnionRename {
37+ /// Use the union class name without changes.
38+ none,
39+
40+ /// Encodes union class named `KebabCase` with a JSON key `kebab-case` .
41+ kebab,
42+
43+ /// Encodes union class named `SnakeCase` with a JSON key `snake_case` .
44+ snake,
45+
46+ /// Encodes union class named `PascalCase` with a JSON key `PascalCase` .
47+ pascal,
48+
49+ /// Encodes union class named `ScreamingSnakeCase` with a JSON key
50+ /// `SCREAMING_SNAKE_CASE`
51+ screamingSnake,
52+ }
53+
3454/// An annotation used to specify a class to generate code for.
3555@JsonSerializable (
3656 checked: true ,
@@ -162,6 +182,20 @@ class JsonSerializable {
162182 /// fields annotated with [JsonKey] .
163183 final FieldRename ? fieldRename;
164184
185+ /// Defines the automatic naming strategy when converting class names
186+ /// to union type names.
187+ ///
188+ /// With a value [UnionRename.none] (the default), the name of the class is
189+ /// used without modification.
190+ ///
191+ /// See [UnionRename] for details on the other options.
192+ final UnionRename ? unionRename;
193+
194+ /// The discriminator key used to identify the union type.
195+ ///
196+ /// Defaults to `type` .
197+ final String ? unionDiscriminator;
198+
165199 /// When `true` on classes with type parameters (generic types), extra
166200 /// "helper" parameters will be generated for `fromJson` and/or `toJson` to
167201 /// support serializing values of those types.
@@ -271,6 +305,8 @@ class JsonSerializable {
271305 this .disallowUnrecognizedKeys,
272306 this .explicitToJson,
273307 this .fieldRename,
308+ this .unionRename,
309+ this .unionDiscriminator,
274310 this .ignoreUnannotated,
275311 this .includeIfNull,
276312 this .converters,
@@ -293,6 +329,8 @@ class JsonSerializable {
293329 disallowUnrecognizedKeys: false ,
294330 explicitToJson: false ,
295331 fieldRename: FieldRename .none,
332+ unionRename: UnionRename .none,
333+ unionDiscriminator: 'type' ,
296334 ignoreUnannotated: false ,
297335 includeIfNull: true ,
298336 genericArgumentFactories: false ,
@@ -314,6 +352,8 @@ class JsonSerializable {
314352 disallowUnrecognizedKeys ?? defaults.disallowUnrecognizedKeys,
315353 explicitToJson: explicitToJson ?? defaults.explicitToJson,
316354 fieldRename: fieldRename ?? defaults.fieldRename,
355+ unionRename: unionRename ?? defaults.unionRename,
356+ unionDiscriminator: unionDiscriminator ?? defaults.unionDiscriminator,
317357 ignoreUnannotated: ignoreUnannotated ?? defaults.ignoreUnannotated,
318358 includeIfNull: includeIfNull ?? defaults.includeIfNull,
319359 genericArgumentFactories:
0 commit comments