Skip to content

Commit 8edea02

Browse files
committed
mostly working
1 parent b6f7ce9 commit 8edea02

File tree

8 files changed

+487
-155
lines changed

8 files changed

+487
-155
lines changed

json_annotation/lib/src/json_serializable.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:collection';
6+
57
class JsonSerializable {
68
final bool createFactory;
79
final bool createToJson;
@@ -50,3 +52,24 @@ class JsonKey {
5052
: this.nullable = nullable ?? true,
5153
this.includeIfNull = includeIfNull;
5254
}
55+
56+
// TODO(kevmoo): Add documentation
57+
abstract class JsonMapWrapper extends MapBase<String, dynamic> {
58+
/// This operation is not supported by an unmodifiable map.
59+
@override
60+
void operator []=(String key, dynamic value) {
61+
throw new UnsupportedError('Cannot modify unmodifiable map.');
62+
}
63+
64+
/// This operation is not supported by an unmodifiable map.
65+
@override
66+
void clear() {
67+
throw new UnsupportedError('Cannot modify unmodifiable map.');
68+
}
69+
70+
/// This operation is not supported by an unmodifiable map.
71+
@override
72+
dynamic remove(Object key) {
73+
throw new UnsupportedError('Cannot modify unmodifiable map.');
74+
}
75+
}

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: 0.2.0
2+
version: 0.2.1-dev
33
description: Annotations for the json_serializable package
44
homepage: https://github.com/dart-lang/json_serializable
55
author: Dart Team <misc@dartlang.org>

json_serializable/example/example.g.dart

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

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class JsonSerializableGenerator
3636

3737
final List<TypeHelper> _typeHelpers;
3838

39+
final bool _doNewThing = true;
40+
3941
/// Creates an instance of [JsonSerializableGenerator].
4042
///
4143
/// If [typeHelpers] is not provided, two built-in helpers are used:
@@ -117,10 +119,13 @@ class JsonSerializableGenerator
117119
}
118120

119121
if (annotation.read('createToJson').boolValue) {
122+
var mixClassName = '${prefix}SerializerMixin';
123+
var helpClassName = '${prefix}JsonMapWrapper';
124+
120125
//
121126
// Generate the mixin class
122127
//
123-
buffer.writeln('abstract class ${prefix}SerializerMixin {');
128+
buffer.writeln('abstract class $mixClassName {');
124129

125130
// write copies of the fields - this allows the toJson method to access
126131
// the fields of the target class
@@ -129,19 +134,84 @@ class JsonSerializableGenerator
129134
buffer.writeln(' ${field.type} get ${field.name};');
130135
}
131136

132-
var includeIfNull = annotation.read('includeIfNull').boolValue;
137+
var classIncludeIfNull = annotation.read('includeIfNull').boolValue;
133138

134-
buffer.writeln(' Map<String, dynamic> toJson() ');
135-
if (fieldsList.every((e) => _includeIfNull(e, includeIfNull))) {
136-
// write simple `toJson` method that includes all keys...
137-
_writeToJsonSimple(buffer, fields.values);
139+
buffer.write(' Map<String, dynamic> toJson() ');
140+
141+
if (_doNewThing) {
142+
buffer.writeln('=> new $helpClassName(this);');
138143
} else {
139-
// At least one field should be excluded if null
140-
_writeToJsonWithNullChecks(buffer, fields.values, includeIfNull);
144+
if (fieldsList.every((e) => _includeIfNull(e, classIncludeIfNull))) {
145+
// write simple `toJson` method that includes all keys...
146+
_writeToJsonSimple(buffer, fields.values);
147+
} else {
148+
// At least one field should be excluded if null
149+
_writeToJsonWithNullChecks(buffer, fields.values, classIncludeIfNull);
150+
}
141151
}
142152

143153
// end of the mixin class
144-
buffer.write('}');
154+
buffer.writeln('}');
155+
156+
if (_doNewThing) {
157+
buffer.writeln();
158+
//
159+
// Generate the Map helper class
160+
//
161+
// TODO(kevmoo): write JsonMapWrapper if annotation lib is prefix-imported
162+
buffer.writeln('''class $helpClassName extends JsonMapWrapper {
163+
final $mixClassName _v;
164+
$helpClassName(this._v);
165+
''');
166+
167+
if (fieldsList.every((e) => _includeIfNull(e, classIncludeIfNull))) {
168+
// write simple `toJson` method that includes all keys...
169+
//_writeToJsonSimple(buffer, fields);
170+
171+
var jsonKeys = fields.values.map(_safeNameAccess).join(', ');
172+
173+
// TODO(kevmoo): maybe put this in a static field instead?
174+
// const lists have unfortunate overhead
175+
buffer.writeln(''' @override
176+
Iterable<String> get keys => const [${jsonKeys}];
177+
''');
178+
} else {
179+
// At least one field should be excluded if null
180+
//_writeToJsonWithNullChecks(buffer, fields, includeIfNull);
181+
buffer.writeln('@override\nIterable<String> get keys sync* {');
182+
183+
for (var field in fields.values) {
184+
var nullCheck = !_includeIfNull(field, classIncludeIfNull);
185+
if (nullCheck) {
186+
buffer.writeln('if (_v.${field.name} != null) {');
187+
}
188+
buffer.writeln('yield ${_safeNameAccess(field)};');
189+
if (nullCheck) {
190+
buffer.writeln('}');
191+
}
192+
}
193+
194+
buffer.writeln('}\n');
195+
}
196+
197+
buffer.writeln('''@override
198+
dynamic operator [](Object key) {
199+
if (key is String) {
200+
switch(key) {
201+
''');
202+
203+
for (var field in fields.values) {
204+
var valueAccess = '_v.${field.name}';
205+
buffer.write('''case ${_safeNameAccess(field)}:
206+
return ${_serializeField(field, accessOverride: valueAccess)};''');
207+
}
208+
209+
buffer.writeln('''
210+
}}
211+
return null;
212+
}''');
213+
}
214+
buffer.writeln('}');
145215
}
146216

147217
return buffer.toString();

json_serializable/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies:
1212

1313
# Use a tight version constraint to ensure that a constraint on
1414
# `json_annotation`. Properly constrains all features it provides.
15-
json_annotation: '>=0.2.0 <0.2.1'
15+
json_annotation: '>=0.2.1 <0.2.2'
1616
path: ^1.3.2
1717
source_gen: ^0.7.0
1818
dev_dependencies:
@@ -21,3 +21,7 @@ dev_dependencies:
2121
collection: ^1.14.0
2222
dart_style: ^1.0.0
2323
test: ^0.12.3
24+
25+
dependency_overrides:
26+
json_annotation:
27+
path: ../json_annotation

0 commit comments

Comments
 (0)