Skip to content

Commit 21775e4

Browse files
committed
fix: review fixes
1 parent 6a65694 commit 21775e4

28 files changed

+113
-215
lines changed

_test_yaml/test/src/build_config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Builder {
8585
Map<String, dynamic> toJson() => _$BuilderToJson(this);
8686
}
8787

88-
@JsonEnum(fieldRename: FieldRename.snake)
88+
@JsonEnum(fieldRename: RenameType.snake)
8989
enum AutoApply { none, dependents, allPackages, rootPackage }
9090

9191
enum BuildTo { cache, source }

example/lib/complex_sealed_class_examples.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import 'package:json_annotation/json_annotation.dart';
22

33
part 'complex_sealed_class_examples.g.dart';
44

5-
@JsonSerializable(
6-
unionDiscriminator: 'organization',
7-
)
5+
@JsonSerializable(unionDiscriminator: 'organization')
86
sealed class Organization {
97
final String name;
108

@@ -16,16 +14,11 @@ sealed class Organization {
1614
Map<String, dynamic> toJson() => _$OrganizationToJson(this);
1715
}
1816

19-
@JsonSerializable(
20-
unionDiscriminator: 'department',
21-
)
17+
@JsonSerializable(unionDiscriminator: 'department')
2218
sealed class Department extends Organization {
2319
final String departmentHead;
2420

25-
Department({
26-
required this.departmentHead,
27-
required super.name,
28-
});
21+
Department({required this.departmentHead, required super.name});
2922

3023
factory Department.fromJson(Map<String, dynamic> json) =>
3124
_$DepartmentFromJson(json);

example/lib/sealed_class_example.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ part 'sealed_class_example.g.dart';
44

55
@JsonSerializable(
66
unionDiscriminator: 'vehicle_type',
7-
unionRename: UnionRename.snake,
7+
unionRename: RenameType.snake,
88
)
99
sealed class Vehicle {
1010
final String vehicleID;
@@ -21,18 +21,12 @@ sealed class Vehicle {
2121
class Car extends Vehicle {
2222
final int numberOfDoors;
2323

24-
Car({
25-
required this.numberOfDoors,
26-
required super.vehicleID,
27-
});
24+
Car({required this.numberOfDoors, required super.vehicleID});
2825
}
2926

3027
@JsonSerializable()
3128
class Bicycle extends Vehicle {
3229
final bool hasBell;
3330

34-
Bicycle({
35-
required this.hasBell,
36-
required super.vehicleID,
37-
});
31+
Bicycle({required this.hasBell, required super.vehicleID});
3832
}

json_annotation/lib/src/allowed_keys_helpers.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ class UnrecognizedUnionTypeException extends BadKeyException {
8989
final Type unionType;
9090

9191
@override
92-
String get message => 'Unrecognized type: $unrecognizedType '
92+
String get message =>
93+
'Unrecognized type: $unrecognizedType '
9394
'for union: $unionType.';
9495

9596
UnrecognizedUnionTypeException(this.unrecognizedType, this.unionType, Map map)
96-
: super._(map);
97+
: super._(map);
9798
}
9899

99100
/// Exception thrown if there are missing required keys in a JSON map that was

json_annotation/lib/src/json_enum.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'json_value.dart';
1212
class JsonEnum {
1313
const JsonEnum({
1414
this.alwaysCreate = false,
15-
this.fieldRename = FieldRename.none,
15+
this.fieldRename = RenameType.none,
1616
this.valueField,
1717
});
1818

@@ -27,14 +27,14 @@ class JsonEnum {
2727
/// Defines the naming strategy when converting enum entry names to JSON
2828
/// values.
2929
///
30-
/// With a value [FieldRename.none] (the default), the name of the enum entry
30+
/// With a value [RenameType.none] (the default), the name of the enum entry
3131
/// is used without modification.
3232
///
33-
/// See [FieldRename] for details on the other options.
33+
/// See [RenameType] for details on the other options.
3434
///
3535
/// Note: the value for [JsonValue.value] takes precedence over this option
3636
/// for entries annotated with [JsonValue].
37-
final FieldRename fieldRename;
37+
final RenameType fieldRename;
3838

3939
/// Specifies the field within an "enhanced enum" to use as the value
4040
/// to use for serialization.

json_annotation/lib/src/json_serializable.dart

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import 'json_key.dart';
1212

1313
part '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

@@ -31,31 +35,11 @@ enum FieldRename {
3135
screamingSnake,
3236
}
3337

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-
5438
/// An annotation used to specify a class to generate code for.
5539
@JsonSerializable(
5640
checked: true,
5741
disallowUnrecognizedKeys: true,
58-
fieldRename: FieldRename.snake,
42+
fieldRename: RenameType.snake,
5943
)
6044
@Target({TargetKind.classType})
6145
class JsonSerializable {
@@ -173,14 +157,14 @@ class JsonSerializable {
173157
/// Defines the automatic naming strategy when converting class field names
174158
/// into JSON map keys.
175159
///
176-
/// 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
177161
/// used without modification.
178162
///
179-
/// See [FieldRename] for details on the other options.
163+
/// See [RenameType] for details on the other options.
180164
///
181165
/// Note: the value for [JsonKey.name] takes precedence over this option for
182166
/// fields annotated with [JsonKey].
183-
final FieldRename? fieldRename;
167+
final RenameType? fieldRename;
184168

185169
/// When `true` on classes with type parameters (generic types), extra
186170
/// "helper" parameters will be generated for `fromJson` and/or `toJson` to
@@ -252,11 +236,11 @@ class JsonSerializable {
252236
/// Defines the automatic naming strategy when converting class names
253237
/// to union type names.
254238
///
255-
/// With a value [UnionRename.none] (the default), the name of the class is
239+
/// With a value [RenameType.none] (the default), the name of the class is
256240
/// used without modification.
257241
///
258-
/// See [UnionRename] for details on the other options.
259-
final UnionRename? unionRename;
242+
/// See [RenameType] for details on the other options.
243+
final RenameType? unionRename;
260244

261245
/// A list of [JsonConverter] to apply to this class.
262246
///
@@ -328,12 +312,12 @@ class JsonSerializable {
328312
createToJson: true,
329313
disallowUnrecognizedKeys: false,
330314
explicitToJson: false,
331-
fieldRename: FieldRename.none,
315+
fieldRename: RenameType.none,
332316
ignoreUnannotated: false,
333317
includeIfNull: true,
334318
genericArgumentFactories: false,
335319
unionDiscriminator: 'type',
336-
unionRename: UnionRename.none,
320+
unionRename: RenameType.none,
337321
);
338322

339323
/// Returns a new [JsonSerializable] instance with fields equal to the

json_annotation/lib/src/json_serializable.g.dart

Lines changed: 7 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/example/sealed_example.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ sealed class SealedBase {
1616
class SealedSub1 extends SealedBase {
1717
final String exampleField1;
1818

19-
SealedSub1({
20-
required this.exampleField1,
21-
});
19+
SealedSub1({required this.exampleField1});
2220
}
2321

2422
@JsonSerializable()
2523
class SealedSub2 extends SealedBase {
2624
final String exampleField2;
2725

28-
SealedSub2({
29-
required this.exampleField2,
30-
});
26+
SealedSub2({required this.exampleField2});
3127
}

json_serializable/lib/src/decode_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ mixin DecodeHelper implements HelperCore {
153153
final discriminator = config.unionDiscriminator;
154154

155155
String buildSingleImpl(ClassElement2 impl) {
156-
final unionName = encodedUnionName(config.unionRename, impl.name3!);
156+
final unionName = encodedName(config.unionRename, impl.name3!);
157157

158158
return "'$unionName' => ${classPrefix(impl)}FromJson(json),";
159159
}

json_serializable/lib/src/encoder_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ mixin EncodeHelper implements HelperCore {
148148
String buildSingleImpl(ClassElement2 impl) {
149149
final originalName = impl.name3!;
150150

151-
final unionName = encodedUnionName(config.unionRename, originalName);
151+
final unionName = encodedName(config.unionRename, originalName);
152152

153153
return '''
154154
final $originalName instance => {

0 commit comments

Comments
 (0)