Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Aug 9, 2024
1 parent 5894d4f commit c49fe29
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions json_serializable/lib/src/encoder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,22 @@ mixin EncodeHelper implements HelperCore {

if (config.genericArgumentFactories) _writeGenericArgumentFactories(buffer);

buffer.write(') ');
buffer
..write(') ')
..writeln('=> <String, dynamic>{')
..writeAll(accessibleFields.map((field) {
final access = _fieldAccess(field);

_writeToJsonSimple(buffer, accessibleFields);
final keyExpression = safeNameAccess(field);
final valueExpression = _serializeField(field, access);

final keyValuePair = _canWriteJsonWithoutNullCheck(field)
? '$keyExpression: $valueExpression'
: 'if ($valueExpression case final $generatedLocalVarName?) '
'$keyExpression: $generatedLocalVarName';
return ' $keyValuePair,\n';
}))
..writeln('};');

yield buffer.toString();
}
Expand All @@ -116,24 +129,6 @@ mixin EncodeHelper implements HelperCore {
}
}

void _writeToJsonSimple(StringBuffer buffer, Iterable<FieldElement> fields) {
buffer
..writeln('=> <String, dynamic>{')
..writeAll(fields.map((field) {
final access = _fieldAccess(field);

final keyExpression = safeNameAccess(field);
final valueExpression = _serializeField(field, access);

final keyValuePair = _canWriteJsonWithoutNullCheck(field)
? '$keyExpression: $valueExpression'
: 'if ($valueExpression case final $generatedLocalVarName?) '
'$keyExpression: $generatedLocalVarName';
return ' $keyValuePair,\n';
}))
..writeln('};');
}

static const _toJsonParamName = 'instance';

String _serializeField(FieldElement field, String accessExpression) {
Expand Down

0 comments on commit c49fe29

Please sign in to comment.