From c49fe2918dd9745bbc7d8a757e6ba2a3bb60e890 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 8 Aug 2024 17:05:48 -0700 Subject: [PATCH] more cleanup --- json_serializable/lib/src/encoder_helper.dart | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/json_serializable/lib/src/encoder_helper.dart b/json_serializable/lib/src/encoder_helper.dart index 732ea7f0c..4e876b757 100644 --- a/json_serializable/lib/src/encoder_helper.dart +++ b/json_serializable/lib/src/encoder_helper.dart @@ -97,9 +97,22 @@ mixin EncodeHelper implements HelperCore { if (config.genericArgumentFactories) _writeGenericArgumentFactories(buffer); - buffer.write(') '); + buffer + ..write(') ') + ..writeln('=> {') + ..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(); } @@ -116,24 +129,6 @@ mixin EncodeHelper implements HelperCore { } } - void _writeToJsonSimple(StringBuffer buffer, Iterable fields) { - buffer - ..writeln('=> {') - ..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) {