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 'ChatBody':
        return ChatBody.fromJson(value);
      case 'ChatBodyContextFilter':
        return ChatBodyContextFilter.fromJson(value);
      case 'Chunk':
        return Chunk.fromJson(value);
      case 'ChunksBody':
        return ChunksBody.fromJson(value);
      case 'ChunksResponse':
        return ChunksResponse.fromJson(value);
      case 'CompletionsBody':
        return CompletionsBody.fromJson(value);
      case 'Content':
        return Content.fromJson(value);
      case 'ContextFilter':
        return ContextFilter.fromJson(value);
      case 'DocMetadata':
        return DocMetadata.fromJson(value);
      case 'DocsIds':
        return DocsIds.fromJson(value);
      case 'Embedding':
        return Embedding.fromJson(value);
      case 'EmbeddingsBody':
        return EmbeddingsBody.fromJson(value);
      case 'EmbeddingsResponse':
        return EmbeddingsResponse.fromJson(value);
      case 'FinishReason':
        return FinishReason.fromJson(value);
      case 'HTTPValidationError':
        return HTTPValidationError.fromJson(value);
      case 'HealthResponse':
        return HealthResponse.fromJson(value);
      case 'IngestResponse':
        return IngestResponse.fromJson(value);
      case 'IngestTextBody':
        return IngestTextBody.fromJson(value);
      case 'IngestedDoc':
        return IngestedDoc.fromJson(value);
      case 'Input':
        return Input.fromJson(value);
      case 'NextTexts':
        return NextTexts.fromJson(value);
      case 'OpenAIChoice':
        return OpenAIChoice.fromJson(value);
      case 'OpenAIChoiceDelta':
        return OpenAIChoiceDelta.fromJson(value);
      case 'OpenAIChoiceMessage':
        return OpenAIChoiceMessage.fromJson(value);
      case 'OpenAICompletion':
        return OpenAICompletion.fromJson(value);
      case 'OpenAIDelta':
        return OpenAIDelta.fromJson(value);
      case 'OpenAIMessage':
        return OpenAIMessage.fromJson(value);
      case 'PreviousTexts':
        return PreviousTexts.fromJson(value);
      case 'Sources':
        return Sources.fromJson(value);
      case 'SystemPrompt':
        return SystemPrompt.fromJson(value);
      case 'ValidationError':
        return ValidationError.fromJson(value);
      case 'ValidationErrorLocInner':
        return ValidationErrorLocInner.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',);
}