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 'Action':
        return Action.fromJson(value);
      case 'Announcement':
        return Announcement.fromJson(value);
      case 'AnnouncementsResponse':
        return AnnouncementsResponse.fromJson(value);
      case 'Batch':
        return Batch.fromJson(value);
      case 'CxDict':
        return CxDict.fromJson(value);
      case 'CxDictTranslationsInner':
        return CxDictTranslationsInner.fromJson(value);
      case 'CxMt':
        return CxMt.fromJson(value);
      case 'DataListsGet200Response':
        return DataListsGet200Response.fromJson(value);
      case 'DataListsIdEntriesBatchPost200Response':
        return DataListsIdEntriesBatchPost200Response.fromJson(value);
      case 'DataListsIdEntriesBatchPost200ResponseBatchInner':
        return DataListsIdEntriesBatchPost200ResponseBatchInner.fromJson(
            value);
      case 'DataListsIdEntriesBatchPostRequest':
        return DataListsIdEntriesBatchPostRequest.fromJson(value);
      case 'DataListsIdEntriesPost200Response':
        return DataListsIdEntriesPost200Response.fromJson(value);
      case 'DataListsPagesProjectTitleGet200Response':
        return DataListsPagesProjectTitleGet200Response.fromJson(value);
      case 'DataListsPost200Response':
        return DataListsPost200Response.fromJson(value);
      case 'DataParsoid':
        return DataParsoid.fromJson(value);
      case 'Feed':
        return Feed.fromJson(value);
      case 'GetListEntries200Response':
        return GetListEntries200Response.fromJson(value);
      case 'Image':
        return Image.fromJson(value);
      case 'ImageDescription':
        return ImageDescription.fromJson(value);
      case 'ListCreateBatch':
        return ListCreateBatch.fromJson(value);
      case 'ListEntryRead':
        return ListEntryRead.fromJson(value);
      case 'ListEntryWrite':
        return ListEntryWrite.fromJson(value);
      case 'ListId':
        return ListId.fromJson(value);
      case 'ListRead':
        return ListRead.fromJson(value);
      case 'ListWrite':
        return ListWrite.fromJson(value);
      case 'Listing':
        return Listing.fromJson(value);
      case 'ListingLinks':
        return ListingLinks.fromJson(value);
      case 'ListingLinksNext':
        return ListingLinksNext.fromJson(value);
      case 'MediaItem':
        return MediaItem.fromJson(value);
      case 'MediaItemCaption':
        return MediaItemCaption.fromJson(value);
      case 'MediaItemOriginal':
        return MediaItemOriginal.fromJson(value);
      case 'MediaList':
        return MediaList.fromJson(value);
      case 'MorelikeResultInner':
        return MorelikeResultInner.fromJson(value);
      case 'Mostread':
        return Mostread.fromJson(value);
      case 'MostreadArticle':
        return MostreadArticle.fromJson(value);
      case 'NewsItem':
        return NewsItem.fromJson(value);
      case 'OnthisdayInner':
        return OnthisdayInner.fromJson(value);
      case 'OnthisdayResponse':
        return OnthisdayResponse.fromJson(value);
      case 'Originalimage':
        return Originalimage.fromJson(value);
      case 'PageSegmentsTitleGet200Response':
        return PageSegmentsTitleGet200Response.fromJson(value);
      case 'Problem':
        return Problem.fromJson(value);
      case 'RecommendationResult':
        return RecommendationResult.fromJson(value);
      case 'RecommendationResultItemsInner':
        return RecommendationResultItemsInner.fromJson(value);
      case 'Related':
        return Related.fromJson(value);
      case 'Result':
        return Result.fromJson(value);
      case 'Revision':
        return Revision.fromJson(value);
      case 'RevisionIdentifier':
        return RevisionIdentifier.fromJson(value);
      case 'RevisionInfo':
        return RevisionInfo.fromJson(value);
      case 'Revisions':
        return Revisions.fromJson(value);
      case 'Summary':
        return Summary.fromJson(value);
      case 'SummaryCoordinates':
        return SummaryCoordinates.fromJson(value);
      case 'Thumbnail':
        return Thumbnail.fromJson(value);
      case 'TitlesSet':
        return TitlesSet.fromJson(value);
      case 'TransformWikitextToLintPostRequest':
        return TransformWikitextToLintPostRequest.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',
  );
}