fromJson static method

dynamic fromJson(
  1. dynamic value,
  2. String targetType, {
  3. bool growable = false,
})

Returns a native instance of an OpenAPI class matching the targetType.

Implementation

static dynamic fromJson(
  dynamic value,
  String targetType, {
  bool growable = false,
}) {
  try {
    switch (targetType) {
      case 'String':
        return value is String ? value : value.toString();
      case 'int':
        return value is int ? value : int.parse('$value');
      case 'double':
        return value is double ? value : double.parse('$value');
      case 'bool':
        if (value is bool) {
          return value;
        }
        final valueString = '$value'.toLowerCase();
        return valueString == 'true' || valueString == '1';
      case 'DateTime':
        return value is DateTime ? value : DateTime.tryParse(value);
      case 'AlarmGroupsResponseData':
        return AlarmGroupsResponseData.fromJson(value);
      case 'AlarmHistGroups':
        return AlarmHistGroups.fromJson(value);
      case 'Alarm':
        return Alarm.fromJson(value);
      case 'AuditActor':
        return AuditActor.fromJson(value);
      case 'AuditObject':
        return AuditObject.fromJson(value);
      case 'AuditRecordRequest':
        return AuditRecordRequest.fromJson(value);
      case 'AuditRecordResponse':
        return AuditRecordResponse.fromJson(value);
      case 'DeviceActiveAlarmGroupsValue':
        return DeviceActiveAlarmGroupsValue.fromJson(value);
      case 'DeviceHistAlarmGroupsValue':
        return DeviceHistAlarmGroupsValue.fromJson(value);
      case 'Level':
        return LevelTypeTransformer().decode(value);
      case 'SchemaField':
        return SchemaField.fromJson(value);
      case 'SchemaRequest':
        return SchemaRequest.fromJson(value);
      case 'SchemaResponse':
        return SchemaResponse.fromJson(value);
      default:
        dynamic match;
        if (value is List &&
            (match = _regList.firstMatch(targetType)?.group(1)) != null) {
          return value
              .map<dynamic>((dynamic v) => fromJson(
                    v,
                    match,
                    growable: growable,
                  ))
              .toList(growable: growable);
        }
        if (value is Set &&
            (match = _regSet.firstMatch(targetType)?.group(1)) != null) {
          return value
              .map<dynamic>((dynamic v) => fromJson(
                    v,
                    match,
                    growable: growable,
                  ))
              .toSet();
        }
        if (value is Map &&
            (match = _regMap.firstMatch(targetType)?.group(1)) != null) {
          return Map<String, dynamic>.fromIterables(
            value.keys.cast<String>(),
            value.values.map<dynamic>((dynamic v) => fromJson(
                  v,
                  match,
                  growable: growable,
                )),
          );
        }
    }
  } on Exception catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.internalServerError,
      'Exception during deserialization.',
      error,
      trace,
    );
  }
  throw ApiException(
    HttpStatus.internalServerError,
    'Could not find a suitable class for deserialization',
  );
}