Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _test_yaml/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dev_dependencies:
build_runner: ^2.10.5
build_verify: ^3.0.0
checked_yaml: ^2.0.4
json_annotation: ^4.9.0
json_annotation: '>=4.13.0-0 <4.14.0'
json_serializable: ^6.8.0
path: ^1.9.0
test: ^1.29.0
Expand Down
2 changes: 1 addition & 1 deletion _test_yaml/test/src/build_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.13.0-wip

- Add `$enumEncode` helper for generated enum `toJson` code.

## 4.12.0

- Add `JsonKey.explicitJsonNullWhenNonNullField` for PATCH-style tri-state JSON
Expand Down
12 changes: 12 additions & 0 deletions json_annotation/lib/src/enum_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ K $enumDecode<K extends Enum, V>(

return unknownValue;
}

/// Returns the encoded JSON value associated with [value] in [enumValues].
///
/// [enumValues] is assumed to contain an entry for every possible value of
/// type [K], so the returned value is never `null`.
///
/// Exposed only for code generated by `package:json_serializable`.
/// Not meant to be used directly by user code.
V $enumEncode<K extends Enum, V extends Object>(
Map<K, V> enumValues,
K value,
) => enumValues[value]!;
2 changes: 1 addition & 1 deletion json_annotation/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_annotation
version: 4.12.0
version: 4.13.0-wip
description: >-
Classes and helper functions that support JSON code generation via the
`json_serializable` package.
Expand Down
5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 6.15.0-wip

- Use `$enumEncode` for non-nullable enum `toJson` map lookups so generated
code no longer triggers `unnecessary_null_checks` on `_$EnumMap[value]!`.
([#1515](https://github.com/google/json_serializable.dart/issues/1515))
- Require `json_annotation: '>=4.13.0-0 <4.14.0'`

- Fix `createJsonSchema` to emit an `enum` constraint (and `default`) for
enum-typed fields, respecting `@JsonValue` and `JsonEnum.valueField`.
([#1577](https://github.com/google/json_serializable.dart/issues/1577))
Expand Down
18 changes: 9 additions & 9 deletions json_serializable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,15 @@ targets:
[`Enum`]: https://api.dart.dev/dart-core/Enum-class.html
[`int`]: https://api.dart.dev/dart-core/int-class.html
[`Iterable`]: https://api.dart.dev/dart-core/Iterable-class.html
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonConverter-class.html
[`JsonEnum.valueField`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonEnum/valueField.html
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonEnum-class.html
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonKey/fromJson.html
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonKey/toJson.html
[`JsonKey`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonKey-class.html
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonLiteral-class.html
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonSerializable-class.html
[`JsonValue`]: https://pub.dev/documentation/json_annotation/4.12.0/json_annotation/JsonValue-class.html
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
[`JsonEnum.valueField`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum/valueField.html
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum-class.html
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
[`JsonKey`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey-class.html
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonLiteral-class.html
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable-class.html
[`JsonValue`]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonValue-class.html
[`List`]: https://api.dart.dev/dart-core/List-class.html
[`Map`]: https://api.dart.dev/dart-core/Map-class.html
[`num`]: https://api.dart.dev/dart-core/num-class.html
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/check_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const _annotationPkgName = 'json_annotation';
final _supportLanguageRange = VersionConstraint.parse(
supportedLanguageConstraint,
);
final requiredJsonAnnotationMinVersion = Version.parse('4.12.0');
final requiredJsonAnnotationMinVersion = Version.parse('4.13.0-0');

Future<void> pubspecHasRightVersion(BuildStep buildStep) async {
final segments = buildStep.inputId.pathSegments;
Expand Down
5 changes: 4 additions & 1 deletion json_serializable/lib/src/type_helpers/enum_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
enumFieldWithNullInEncodeMap(targetType) == true) {
return '${constMapName(targetType)}[$expression]';
} else {
return '${constMapName(targetType)}[$expression]!';
// Avoid `_$EnumMap[value]!` which triggers `unnecessary_null_checks` when
// the result is stored in a `Map<String, dynamic>` (#1515), while still
// producing a non-nullable value for Map keys (#1145).
return '\$enumEncode(${constMapName(targetType)}, $expression)';
}
}

Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:

# Use a tight version constraint to ensure that a constraint on
# `json_annotation` properly constrains all features it provides.
json_annotation: '>=4.12.0 <4.13.0'
json_annotation: '>=4.13.0-0 <4.14.0'
meta: ^1.16.0
path: ^1.9.0
pub_semver: ^2.1.4
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/test/default_value/default_value.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ void main() {
expect(dayTypeEnumValues, ['no-good', 'rotten', 'very-bad']);
});

test('non-nullable enum toJson uses enumEncode without a bang', () {
final json = Issue1515Regression(
state: Issue1145RegressionEnum.alpha,
).toJson();
expect(json, {'state': 'alpha'});
expect(json['state'], const TypeMatcher<String>());
});

test('serializing a non-nullable enum as a key in a map should produce a '
'non-nullable string key', () {
final cls = Issue1145RegressionA(
Expand Down
9 changes: 9 additions & 0 deletions json_serializable/test/integration/json_enum_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ class Issue1226Regression {

Map<String, dynamic> toJson() => _$Issue1226RegressionToJson(this);
}

@JsonSerializable(createFactory: false)
class Issue1515Regression {
Issue1515Regression({required this.state});

Map<String, dynamic> toJson() => _$Issue1515RegressionToJson(this);

final Issue1145RegressionEnum state;
}
8 changes: 7 additions & 1 deletion json_serializable/test/integration/json_enum_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions json_serializable/test/integration/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions json_serializable/test/integration/schema_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions json_serializable/test/supported_types/input.type_list.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading