Skip to content

Centralize error handling for calls to _serialize #49

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

Merged
merged 1 commit into from
Oct 3, 2017
Merged
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
87 changes: 43 additions & 44 deletions json_serializable/lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ class JsonSerializableGenerator
return buffer.toString();
}

void _writeToJsonWithNullChecks(
StringBuffer buffer, Iterable<FieldElement> fields, bool includeIfNull) {
void _writeToJsonWithNullChecks(StringBuffer buffer,
Iterable<FieldElement> fields, bool classIncludeIfNull) {
buffer.writeln('{');

buffer.writeln('var $toJsonMapVarName = <String, dynamic>{');
Expand All @@ -160,48 +160,42 @@ class JsonSerializableGenerator
var directWrite = true;

for (var field in fields) {
var fieldName = field.name;
try {
var safeJsonKeyString = _safeNameAccess(field);

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

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

if (_includeIfNull(field, includeIfNull)) {
if (directWrite) {
buffer.writeln('$safeJsonKeyString : '
'${_serialize(field.type, fieldName, _nullable(field))},');
} else {
buffer.writeln('$toJsonMapVarName[$safeJsonKeyString] = '
'${_serialize(field.type, fieldName, _nullable(field))};');
}
if (_includeIfNull(field, classIncludeIfNull)) {
if (directWrite) {
buffer.writeln('$safeJsonKeyString : '
'${_serializeField(field, accessOverride: safeFieldAccess)},');
} else {
if (directWrite) {
// close the still-open map literal
buffer.writeln('};');
buffer.writeln();

// write the helper to be used by all following null-excluding
// fields
buffer.writeln('''
buffer.writeln('$toJsonMapVarName[$safeJsonKeyString] = '
'${_serializeField(field, accessOverride: safeFieldAccess)};');
}
} else {
if (directWrite) {
// close the still-open map literal
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))});');
directWrite = false;
}
} on UnsupportedTypeError {
throw new InvalidGenerationSourceError(
'Could not generate `toJson` code for `${friendlyNameForElement(
field)}`.',
todo: 'Make sure all of the types are serializable.');
buffer.writeln('$toJsonMapHelperName($safeJsonKeyString, '
'${_serializeField(field, accessOverride: safeFieldAccess)});');
}
}

Expand All @@ -215,14 +209,7 @@ void $toJsonMapHelperName(String key, dynamic value) {

var pairs = <String>[];
for (var field in fields) {
try {
pairs.add('${_safeNameAccess(field)}: '
'${_serialize(field.type, field.name, _nullable(field))}');
} on UnsupportedTypeError {
throw new InvalidGenerationSourceError(
'Could not generate `toJson` code for `${friendlyNameForElement(field)}`.',
todo: 'Make sure all of the types are serializable.');
}
pairs.add('${_safeNameAccess(field)}: ${_serializeField(field )}');
}
buffer.writeAll(pairs, ',\n');

Expand Down Expand Up @@ -334,6 +321,18 @@ void $toJsonMapHelperName(String key, dynamic value) {
Iterable<TypeHelper> get _allHelpers =>
[_typeHelpers, _coreHelpers].expand((e) => e);

String _serializeField(FieldElement field, {String accessOverride}) {
accessOverride ??= field.name;
try {
return _serialize(field.type, accessOverride, _nullable(field));
} on UnsupportedTypeError {
throw new InvalidGenerationSourceError(
'Could not generate `toJson` code for '
'`${friendlyNameForElement(field)}`.',
todo: 'Make sure all of the types are serializable.');
}
}

/// [expression] may be just the name of the field or it may an expression
/// representing the serialization of a value.
String _serialize(DartType targetType, String expression, bool nullable) =>
Expand Down