generateToJson method
Implementation
String generateToJson(String name, Iterable<Field> fields) {
final result = StringBuffer();
result.writeln(' Map<String, dynamic> toJson() {');
result.writeln(' final \$result = <String, dynamic>{};');
result.writeln();
for (final field in fields) {
if (field is TypeAdapterField) {
if (field.type.isOptional) {
result.writeln(
' \$result[\'${field.jsonName}\'] = ${field.name} == null ? null : const ${field.adapterType.displayName}().toJson(${field.name}!);',
);
} else {
result.writeln(
' \$result[\'${field.jsonName}\'] = const ${field.adapterType.displayName}().toJson(${field.name});',
);
}
} else {
final writer = _jsonSetter(field.type, field.name, field.jsonName);
result.writeln(' \$result[\'${field.jsonName}\'] = $writer;');
}
}
result.writeln();
result.writeln(' return \$result;');
result.writeln(' }');
return result.toString();
}