Skip to content

WIP: remove toJson helper – not sure I like this... #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 13 additions & 15 deletions example/example.g.dart

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

18 changes: 7 additions & 11 deletions lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class JsonSerializableGenerator

// If `fieldName` collides with one of the local helpers, prefix
// access with `this.`.
if (fieldName == toJsonMapVarName || fieldName == toJsonMapHelperName) {
if (fieldName == toJsonMapVarName) {
fieldName = 'this.$fieldName';
}

Expand All @@ -184,18 +184,14 @@ class JsonSerializableGenerator
buffer.writeln('};');
buffer.writeln();

// write the helper to be used by all following null-excluding
// fields
buffer.writeln('''
void $toJsonMapHelperName(String key, dynamic value) {
if (value != null) {
$toJsonMapVarName[key] = value;
}
}''');
directWrite = false;
}
buffer.writeln('$toJsonMapHelperName($safeJsonKeyString, '
'${_serialize(field.type, fieldName, _nullable(field))});');
buffer.writeln('''if ($fieldName != null) {
$toJsonMapVarName[$safeJsonKeyString] = ${_serialize(field.type, fieldName, _nullable(field))};
}''');

//$toJsonMapHelperName($safeJsonKeyString, '
//'${_serialize(field.type, fieldName, _nullable(field))});');
}
} on UnsupportedTypeError {
throw new InvalidGenerationSourceError(
Expand Down
1 change: 0 additions & 1 deletion lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ void log(object) {
}

final toJsonMapVarName = 'val';
final toJsonMapHelperName = 'writeNotNull';
2 changes: 0 additions & 2 deletions test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,11 @@ abstract class _$OrderSerializerMixin {
test('some', () async {
var output = await _runForElementNamed('IncludeIfNullAll');
expect(output, isNot(contains(toJsonMapVarName)));
expect(output, isNot(contains(toJsonMapHelperName)));
});

test('all', () async {
var output = await _runForElementNamed('IncludeIfNullOverride');
expect(output, contains("'number': number,"));
expect(output, contains("$toJsonMapHelperName('str', str);"));
});
});
}
Expand Down
1 change: 0 additions & 1 deletion test/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,5 @@ final _expectedOrder = [
'stringDateTimeMap',
'crazyComplex',
toJsonMapVarName,
toJsonMapHelperName,
r'$string'
];
2 changes: 0 additions & 2 deletions test/test_files/bathtub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class Bathtub extends Object
// Handle fields with names that collide with helper names
@JsonKey(nullable: false, includeIfNull: false)
Map<String, bool> val = {};
@JsonKey(nullable: false)
bool writeNotNull;
@JsonKey(nullable: false, name: r'$string')
String string;

Expand Down
11 changes: 2 additions & 9 deletions test/test_files/bathtub.g.dart

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

1 change: 0 additions & 1 deletion test/test_files/kitchen_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class KitchenSink extends Object with _$KitchenSinkSerializerMixin {
// Handle fields with names that collide with helper names
@JsonKey(includeIfNull: false)
Map<String, bool> val;
bool writeNotNull;
@JsonKey(name: r'$string')
String string;

Expand Down
58 changes: 28 additions & 30 deletions test/test_files/kitchen_sink.g.dart

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