fromJsonFunction property
String
get
fromJsonFunction
Generates the fromJson
factory constructor code.
Creates a factory constructor that can deserialize JSON data into
the Dart class instance. Handles special cases like JSON/JSONB columns
which require jsonDecode
parsing.
Returns the complete factory constructor code as a string.
Implementation
String get fromJsonFunction {
final buffer = StringBuffer();
buffer.writeln("factory $className.fromJson(Map<String, dynamic> json) {");
buffer.writeln("return $className(");
for (var element in properties) {
if (element.type == 'json' || element.type == 'jsonb') {
final command =
'''
jsonDecode(json['${element.name}'].toString()) as Map<String, dynamic>
''';
if (element.isNullable) {
buffer.writeln(
"${element.dartName}: json['${element.name}'] != null ? $command: null,",
);
} else {
buffer.writeln("${element.dartName}: $command,");
}
} else {
buffer.writeln("${element.dartName}: json['${element.name}'],");
}
}
buffer.writeln(");");
buffer.writeln("}");
return buffer.toString();
}