Skip to content

Commit 8aadb45

Browse files
rrousselGitkevmoo
andauthored
Add JsonConverter(converters) option (#1135)
Co-authored-by: Kevin Moore <[email protected]>
1 parent 51224ed commit 8aadb45

28 files changed

+314
-169
lines changed

checked_yaml/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ dev_dependencies:
2222
test_process: ^2.0.0
2323

2424
dependency_overrides:
25+
json_annotation:
26+
path: ../json_annotation
2527
json_serializable:
2628
path: ../json_serializable

json_annotation/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.6.0
2+
3+
- Added `JsonSerializable(converters: <JsonConverter>[])`
4+
([#1072](https://github.com/google/json_serializable.dart/issues/1072))
5+
16
## 4.5.0
27

38
- Added `FieldRename.screamingSnake`.
@@ -38,7 +43,7 @@
3843

3944
## 4.0.1
4045

41-
- Fix a potential error with `checked: true` when `ArgumentError.message` is
46+
- Fix a potential error with `checked: true` when `ArgumentError.message` is
4247
`null`.
4348
- Updated `JsonSerializable.fromJson` to handle `null` values.
4449
- Deprecate `JsonSerializable` `defaults` and `withDefaults()`.

json_annotation/lib/src/json_converter.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@
88
///
99
/// [S] is the type of the value stored in JSON. It must be a valid JSON type
1010
/// such as [String], [int], or [Map<String, dynamic>].
11+
///
12+
///
13+
/// [JsonConverter]s can be placed either on the class:
14+
///
15+
/// ```dart
16+
/// class MyConverter extends JsonConverter<Value, JSON> {
17+
/// // TODO
18+
/// }
19+
///
20+
/// @JsonSerializable()
21+
/// @MyJsonConverter()
22+
/// class Example {}
23+
/// ```
24+
///
25+
/// or on a property:
26+
///
27+
/// ```dart
28+
/// @JsonSerializable()
29+
/// @MyJsonConverter()
30+
/// class Example {
31+
/// @MyJsonConverter()
32+
/// final Value property;
33+
/// }
34+
/// ```
35+
///
36+
/// Or finally, passed to the annotation:
37+
///
38+
///```dart
39+
/// @JsonSerializable(converters: [MyConverter()])
40+
/// class Example {}
41+
/// ```
1142
abstract class JsonConverter<T, S> {
1243
const JsonConverter();
1344

json_annotation/lib/src/json_serializable.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:meta/meta_meta.dart';
66

77
import 'allowed_keys_helpers.dart';
88
import 'checked_helpers.dart';
9+
import 'json_converter.dart';
910
import 'json_key.dart';
1011

1112
part 'json_serializable.g.dart';
@@ -190,6 +191,40 @@ class JsonSerializable {
190191
/// `includeIfNull`, that value takes precedent.
191192
final bool? includeIfNull;
192193

194+
/// A list of [JsonConverter] to apply to this class.
195+
///
196+
/// Writing:
197+
///
198+
/// ```dart
199+
/// @JsonSerializable(converters: [MyJsonConverter()])
200+
/// class Example {...}
201+
/// ```
202+
///
203+
/// is equivalent to writing:
204+
///
205+
/// ```dart
206+
/// @JsonSerializable()
207+
/// @MyJsonConverter()
208+
/// class Example {...}
209+
/// ```
210+
///
211+
/// The main difference is that this allows reusing a custom
212+
/// [JsonSerializable] over multiple classes:
213+
///
214+
/// ```dart
215+
/// const myCustomAnnotation = JsonSerializable(
216+
/// converters: [MyJsonConverter()],
217+
/// );
218+
///
219+
/// @myCustomAnnotation
220+
/// class Example {...}
221+
///
222+
/// @myCustomAnnotation
223+
/// class Another {...}
224+
/// ```
225+
@JsonKey(ignore: true)
226+
final List<JsonConverter>? converters;
227+
193228
/// Creates a new [JsonSerializable] instance.
194229
const JsonSerializable({
195230
@Deprecated('Has no effect') bool? nullable,
@@ -203,6 +238,7 @@ class JsonSerializable {
203238
this.fieldRename,
204239
this.ignoreUnannotated,
205240
this.includeIfNull,
241+
this.converters,
206242
this.genericArgumentFactories,
207243
});
208244

json_annotation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_annotation
2-
version: 4.5.0
2+
version: 4.6.0
33
description: >-
44
Classes and helper functions that support JSON code generation via the
55
`json_serializable` package.

json_serializable/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Added support for using a `JsonConverter<MyClass, Object>` on properties
44
of type `MyClass?`. ([#822](https://github.com/google/json_serializable.dart/issues/822))
5+
- Added support for `JsonSerializable(converters: <JsonConverter>[])`
6+
([#1072](https://github.com/google/json_serializable.dart/issues/1072))
57

68
## 6.2.0
79

json_serializable/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ targets:
199199
[`Enum`]: https://api.dart.dev/stable/dart-core/Enum-class.html
200200
[`int`]: https://api.dart.dev/stable/dart-core/int-class.html
201201
[`Iterable`]: https://api.dart.dev/stable/dart-core/Iterable-class.html
202-
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonConverter-class.html
203-
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonEnum-class.html
204-
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonKey/fromJson.html
205-
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonKey/toJson.html
206-
[`JsonKey`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonKey-class.html
207-
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonLiteral-class.html
208-
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonSerializable-class.html
209-
[`JsonValue`]: https://pub.dev/documentation/json_annotation/4.5.0/json_annotation/JsonValue-class.html
202+
[`JsonConverter`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonConverter-class.html
203+
[`JsonEnum`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonEnum-class.html
204+
[`JsonKey.fromJson`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonKey/fromJson.html
205+
[`JsonKey.toJson`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonKey/toJson.html
206+
[`JsonKey`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonKey-class.html
207+
[`JsonLiteral`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonLiteral-class.html
208+
[`JsonSerializable`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonSerializable-class.html
209+
[`JsonValue`]: https://pub.dev/documentation/json_annotation/4.6.0/json_annotation/JsonValue-class.html
210210
[`List`]: https://api.dart.dev/stable/dart-core/List-class.html
211211
[`Map`]: https://api.dart.dev/stable/dart-core/Map-class.html
212212
[`num`]: https://api.dart.dev/stable/dart-core/num-class.html

json_serializable/lib/src/check_dependencies.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:pubspec_parse/pubspec_parse.dart';
1010

1111
const _productionDirectories = {'lib', 'bin'};
1212
const _annotationPkgName = 'json_annotation';
13-
final requiredJsonAnnotationMinVersion = Version.parse('4.5.0');
13+
final requiredJsonAnnotationMinVersion = Version.parse('4.6.0');
1414

1515
Future<void> pubspecHasRightVersion(BuildStep buildStep) async {
1616
final segments = buildStep.inputId.pathSegments;

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class JsonSerializableGenerator
2020
extends GeneratorForAnnotation<JsonSerializable> {
2121
final Settings _settings;
2222

23-
JsonSerializable get config => _settings.config;
23+
JsonSerializable get config => _settings.config.toJsonSerializable();
2424

2525
JsonSerializableGenerator.fromSettings(this._settings);
2626

json_serializable/lib/src/settings.dart

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,19 @@ class Settings {
4343
GenericFactoryHelper(),
4444
].followedBy(_typeHelpers).followedBy(_coreHelpers);
4545

46-
final JsonSerializable _config;
47-
48-
// #CHANGE WHEN UPDATING json_annotation
49-
ClassConfig get config => ClassConfig(
50-
checked: _config.checked ?? ClassConfig.defaults.checked,
51-
anyMap: _config.anyMap ?? ClassConfig.defaults.anyMap,
52-
constructor: _config.constructor ?? ClassConfig.defaults.constructor,
53-
createFactory:
54-
_config.createFactory ?? ClassConfig.defaults.createFactory,
55-
createToJson: _config.createToJson ?? ClassConfig.defaults.createToJson,
56-
ignoreUnannotated:
57-
_config.ignoreUnannotated ?? ClassConfig.defaults.ignoreUnannotated,
58-
explicitToJson:
59-
_config.explicitToJson ?? ClassConfig.defaults.explicitToJson,
60-
includeIfNull:
61-
_config.includeIfNull ?? ClassConfig.defaults.includeIfNull,
62-
genericArgumentFactories: _config.genericArgumentFactories ??
63-
ClassConfig.defaults.genericArgumentFactories,
64-
fieldRename: _config.fieldRename ?? ClassConfig.defaults.fieldRename,
65-
disallowUnrecognizedKeys: _config.disallowUnrecognizedKeys ??
66-
ClassConfig.defaults.disallowUnrecognizedKeys,
67-
);
46+
final ClassConfig config;
6847

6948
/// Creates an instance of [Settings].
7049
///
7150
/// If [typeHelpers] is not provided, the built-in helpers are used:
7251
/// [BigIntHelper], [DateTimeHelper], [DurationHelper], [JsonHelper], and
7352
/// [UriHelper].
74-
const Settings({
53+
Settings({
7554
JsonSerializable? config,
7655
List<TypeHelper>? typeHelpers,
77-
}) : _config = config ?? ClassConfig.defaults,
56+
}) : config = config != null
57+
? ClassConfig.fromJsonSerializable(config)
58+
: ClassConfig.defaults,
7859
_typeHelpers = typeHelpers ?? defaultHelpers;
7960

8061
/// Creates an instance of [Settings].

0 commit comments

Comments
 (0)