jsonParseExpression method

String jsonParseExpression(
  1. String key,
  2. bool privateField
)

Implementation

String jsonParseExpression(String key, bool privateField) {
  final jsonKey = "json['$key']";
  final fieldKey =
      fixFieldName(key, typeDef: this, privateField: privateField);
  if (isPrimitive) {
    if (name == "List") {
      return "$fieldKey = json['$key'].cast<$subtype>();";
    }
    return "$fieldKey = json['$key'];";
  } else if (name == "List" && subtype == "DateTime") {
    return "$fieldKey = json['$key'].map((v) => DateTime.tryParse(v));";
  } else if (name == "DateTime") {
    return "$fieldKey = DateTime.tryParse(json['$key']);";
  } else if (name == 'List') {
    // list of class
    return "if (json['$key'] != null) {\n\t\t\t$fieldKey = <$subtype>[];\n\t\t\tjson['$key'].forEach((v) { $fieldKey!.add(new $subtype.fromJson(v)); });\n\t\t}";
  } else {
    // class
    return "$fieldKey = json['$key'] != null ? ${_buildParseClass(jsonKey)} : null;";
  }
}