fromJsonBody method

String fromJsonBody()

Implementation

String fromJsonBody() {
  return checkNestedTypes(type!, (String cleanedType, bool isList, bool isListInList, bool isModel) {
    final jsonVar = "json['$originalName']";
    String conversion;
    String modelFromJson([String jsonVar = 'e']) => '$cleanedType.fromJson($jsonVar as Map<String, dynamic>)';

    if (isListInList) {
      conversion =
          '($jsonVar as List? ?? []).map((e) => (e as List? ?? []).map((e) => ${modelFromJson()}).toList()).toList()';
    } else if (isList) {
      if (isModel) {
        conversion = '($jsonVar as List? ?? []).map((e) => ${modelFromJson()}).toList()';
      } else {
        conversion = '($jsonVar as List? ?? []).map((e) => e as $cleanedType).toList()';
      }
    } else if (isModel) {
      conversion = modelFromJson(jsonVar);
    } else if (isDatetime && !isTimeStamp) {
      conversion = 'DateTime.parse($jsonVar as String)';
    } else if (isTimeStamp) {
      conversion = 'DateTime.fromMillisecondsSinceEpoch($jsonVar as int)';
    } else if (type == 'String') {
      conversion = '$jsonVar$isNullableString.toString()';
    } else if (type == 'double') {
      conversion = '($jsonVar as num).toDouble()';
    } else if (explicitTypeOverride && isJsonable) {
      conversion = modelFromJson(jsonVar);
    } else {
      conversion = '$jsonVar as $type';
    }

    if (isNullable && type != 'String') {
      return '$name: $jsonVar != null ? $conversion : null';
    } else {
      return '$name: $conversion';
    }
  });
}