generateUnsafeDart method
generateUnsafeDart will generate all classes and append one after another
in a single string. The rawJson
param is assumed to be a properly
formatted JSON string. The dart code is not validated so invalid dart code
might be returned
Implementation
DartCode generateUnsafeDart(String rawJson) {
final jsonRawData = decodeJSON(rawJson);
final astNode = parse(rawJson, Settings());
var warnings =
_generateClassDefinition(_rootClassName, jsonRawData, '', astNode);
// after generating all classes, replace the omited similar classes.
for (var c in allClasses) {
final fieldsKeys = c.fields.keys;
for (var f in fieldsKeys) {
final typeForField = c.fields[f]!;
var fieldName = typeForField.name;
if (sameClassMapping.containsKey(fieldName)) {
c.fields[f]!.name = sameClassMapping[fieldName];
}
// check subtype for list
if (fieldName == 'List') {
fieldName = PubspecUtils.nullSafeSupport
? '${typeForField.subtype}?'
: typeForField.subtype;
if (sameClassMapping.containsKey(fieldName)) {
c.fields[f]!.subtype =
sameClassMapping[fieldName]!.replaceAll('?', '');
}
}
}
}
return DartCode(allClasses.map((c) => c.toString()).join('\n'), warnings);
}