generateFromJson method
Implementation
String generateFromJson(String name, Iterable<Field> fields) {
final result = StringBuffer();
result.writeln(' $name.fromJson(Map<String, dynamic> json)');
result.write(' : ');
final margin = ' ' * 8;
for (final field in fields) {
if (field is UnionField) {
result.writeln('${field.name} = _\$createUnion\$${field.name}(json),');
result.write(margin);
} else if (field is TypeAdapterField) {
result.writeln('${field.name} = _\$createTypeAdapter\$${field.name}(json),');
result.write(margin);
} else {
result.writeln('${field.name} = ' + _jsonGetter(field.type, field.jsonName, 'json') + ',');
result.write(margin);
}
}
// Remove last margin and last comma
final fromJson = result.toString().substring(0, result.length - margin.length - 2);
result.clear();
result.write(fromJson);
result.writeln();
result.writeln(' {');
result.write(generateValidators(fields));
result.writeln(' }');
return result.toString();
}