Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Aug 8, 2024
1 parent de7da92 commit 5894d4f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion json_serializable/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// Name used for closure argument when generating calls to `map`.
const closureArg = 'e';

const localVal = 'val';
const generatedLocalVarName = 'val';

const converterOrKeyInstructions = r'''
* Use `JsonConverter`
Expand Down
9 changes: 6 additions & 3 deletions json_serializable/lib/src/encoder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ mixin EncodeHelper implements HelperCore {
..writeAll(fields.map((field) {
final access = _fieldAccess(field);

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

final keyValuePair = _canWriteJsonWithoutNullCheck(field)
? '${safeNameAccess(field)}: ${_serializeField(field, access)}'
: 'if (${_serializeField(field, access)} case final $localVal?) '
'${safeNameAccess(field)}: $localVal';
? '$keyExpression: $valueExpression'
: 'if ($valueExpression case final $generatedLocalVarName?) '
'$keyExpression: $generatedLocalVarName';
return ' $keyValuePair,\n';
}))
..writeln('};');
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/test/custom_configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Map<String, dynamic> _$TrivialNestedNonNullableToJson(
group('includeIfNull', () {
test('some', () async {
final output = await runForElementNamed('IncludeIfNullAll');
expect(output, isNot(contains(localVal)));
expect(output, isNot(contains(generatedLocalVarName)));
});
});
}
Expand Down

0 comments on commit 5894d4f

Please sign in to comment.