Skip to content

Commit 25c6138

Browse files
authored
feat: Add JsonSerializable(createJsonMeta) (#1164)
As discussed before, this adds an option for generating a constant Map of property name: json name fixes #972
1 parent 0f383e7 commit 25c6138

17 files changed

+161
-39
lines changed

json_annotation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 4.6.0
22

3+
- Added `JsonSerializable(createFieldMap: true)`.
4+
([#1164](https://github.com/google/json_serializable.dart/pull/1164))
35
- Added `JsonSerializable(converters: <JsonConverter>[])`
46
([#1072](https://github.com/google/json_serializable.dart/issues/1072))
57

json_annotation/lib/src/json_serializable.dart

Lines changed: 9 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 'enum_helpers.dart';
910
import 'json_converter.dart';
1011
import 'json_key.dart';
1112

@@ -79,6 +80,13 @@ class JsonSerializable {
7980
/// ```
8081
final bool? createFactory;
8182

83+
/// If `true` (defaults to false), a private, static `_$ExampleJsonMeta`
84+
/// constant is created in the generated part file.
85+
///
86+
/// This constant can be used by other code-generators to support features
87+
/// such as [fieldRename].
88+
final bool? createFieldMap;
89+
8290
/// If `true` (the default), A top-level function is created that you can
8391
/// reference from your class.
8492
///
@@ -231,6 +239,7 @@ class JsonSerializable {
231239
this.anyMap,
232240
this.checked,
233241
this.constructor,
242+
this.createFieldMap,
234243
this.createFactory,
235244
this.createToJson,
236245
this.disallowUnrecognizedKeys,

json_annotation/lib/src/json_serializable.g.dart

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

json_serializable/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 6.3.1-dev
2+
3+
- Added support for generating `_$ExampleFieldMeta`, which can be used by other
4+
code-generators that needs to interact with the JSON serialization.
5+
([#1164](https://github.com/google/json_serializable.dart/pull/1164))
6+
17
## 6.3.0-dev
28

39
- Added support for using a `JsonConverter<MyClass, Object>` on properties

json_serializable/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ targets:
179179
checked: false
180180
constructor: ""
181181
create_factory: true
182+
create_field_map: false
182183
create_to_json: true
183184
disallow_unrecognized_keys: false
184185
explicit_to_json: false

json_serializable/lib/src/encoder_helper.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ import 'unsupported_type_error.dart';
1616
abstract class EncodeHelper implements HelperCore {
1717
String _fieldAccess(FieldElement field) => '$_toJsonParamName.${field.name}';
1818

19+
/// Generates an object containing metadatas related to the encoding,
20+
/// destined to be used by other code-generators.
21+
String createFieldMap(Set<FieldElement> accessibleFieldSet) {
22+
assert(config.createFieldMap);
23+
24+
final buffer = StringBuffer(
25+
'const _\$${element.name.nonPrivate}FieldMap = <String, String> {',
26+
);
27+
28+
for (final field in accessibleFieldSet) {
29+
buffer.writeln(
30+
'${escapeDartString(field.name)}: '
31+
'${escapeDartString(nameAccess(field))},',
32+
);
33+
}
34+
35+
buffer.write('};');
36+
37+
return buffer.toString();
38+
}
39+
1940
Iterable<String> createToJson(Set<FieldElement> accessibleFields) sync* {
2041
assert(config.createToJson);
2142

json_serializable/lib/src/generator_helper.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class GeneratorHelper extends HelperCore with EncodeHelper, DecodeHelper {
108108
},
109109
);
110110

111+
if (config.createFieldMap) {
112+
yield createFieldMap(accessibleFieldSet);
113+
}
114+
111115
if (config.createToJson) {
112116
yield* createToJson(accessibleFieldSet);
113117
}

json_serializable/lib/src/type_helpers/config_types.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ClassConfig {
4545
final String constructor;
4646
final bool createFactory;
4747
final bool createToJson;
48+
final bool createFieldMap;
4849
final bool disallowUnrecognizedKeys;
4950
final bool explicitToJson;
5051
final FieldRename fieldRename;
@@ -60,6 +61,7 @@ class ClassConfig {
6061
required this.constructor,
6162
required this.createFactory,
6263
required this.createToJson,
64+
required this.createFieldMap,
6365
required this.disallowUnrecognizedKeys,
6466
required this.explicitToJson,
6567
required this.fieldRename,
@@ -76,6 +78,8 @@ class ClassConfig {
7678
checked: config.checked ?? ClassConfig.defaults.checked,
7779
anyMap: config.anyMap ?? ClassConfig.defaults.anyMap,
7880
constructor: config.constructor ?? ClassConfig.defaults.constructor,
81+
createFieldMap:
82+
config.createFieldMap ?? ClassConfig.defaults.createFieldMap,
7983
createFactory:
8084
config.createFactory ?? ClassConfig.defaults.createFactory,
8185
createToJson: config.createToJson ?? ClassConfig.defaults.createToJson,
@@ -101,6 +105,7 @@ class ClassConfig {
101105
constructor: '',
102106
createFactory: true,
103107
createToJson: true,
108+
createFieldMap: false,
104109
disallowUnrecognizedKeys: false,
105110
explicitToJson: false,
106111
fieldRename: FieldRename.none,
@@ -115,6 +120,7 @@ class ClassConfig {
115120
constructor: constructor,
116121
createFactory: createFactory,
117122
createToJson: createToJson,
123+
createFieldMap: createFieldMap,
118124
ignoreUnannotated: ignoreUnannotated,
119125
explicitToJson: explicitToJson,
120126
includeIfNull: includeIfNull,

json_serializable/lib/src/utils.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ JsonSerializable _valueForAnnotation(ConstantReader reader) => JsonSerializable(
5656
constructor: reader.read('constructor').literalValue as String?,
5757
createFactory: reader.read('createFactory').literalValue as bool?,
5858
createToJson: reader.read('createToJson').literalValue as bool?,
59+
createFieldMap: reader.read('createFieldMap').literalValue as bool?,
5960
disallowUnrecognizedKeys:
6061
reader.read('disallowUnrecognizedKeys').literalValue as bool?,
6162
explicitToJson: reader.read('explicitToJson').literalValue as bool?,
@@ -101,6 +102,7 @@ ClassConfig mergeConfig(
101102
constructor: constructor,
102103
createFactory: annotation.createFactory ?? config.createFactory,
103104
createToJson: annotation.createToJson ?? config.createToJson,
105+
createFieldMap: annotation.createFieldMap ?? config.createFieldMap,
104106
disallowUnrecognizedKeys:
105107
annotation.disallowUnrecognizedKeys ?? config.disallowUnrecognizedKeys,
106108
explicitToJson: annotation.explicitToJson ?? config.explicitToJson,

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.3.0-dev
2+
version: 6.3.1-dev
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

0 commit comments

Comments
 (0)