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 'ApiKey':
        return ApiKey.fromJson(value);
      case 'ApiV1ApiKeysGet200Response':
        return ApiV1ApiKeysGet200Response.fromJson(value);
      case 'ApiV1ApiKeysPost200Response':
        return ApiV1ApiKeysPost200Response.fromJson(value);
      case 'ApiV1BillingPortalPost200Response':
        return ApiV1BillingPortalPost200Response.fromJson(value);
      case 'ApiV1BillingProductsGet200Response':
        return ApiV1BillingProductsGet200Response.fromJson(value);
      case 'ApiV1BillingSubscribePost200Response':
        return ApiV1BillingSubscribePost200Response.fromJson(value);
      case 'ApiV1LicensesEntitlementsGet200Response':
        return ApiV1LicensesEntitlementsGet200Response.fromJson(value);
      case 'ApiV1LicensesGet200Response':
        return ApiV1LicensesGet200Response.fromJson(value);
      case 'ApiV1OauthClientsGet200Response':
        return ApiV1OauthClientsGet200Response.fromJson(value);
      case 'ApiV1OauthClientsPost201Response':
        return ApiV1OauthClientsPost201Response.fromJson(value);
      case 'ApiV1OrganizationsGet200Response':
        return ApiV1OrganizationsGet200Response.fromJson(value);
      case 'ApiV1OrganizationsIdInvitePost201Response':
        return ApiV1OrganizationsIdInvitePost201Response.fromJson(value);
      case 'ApiV1ProductsGet200Response':
        return ApiV1ProductsGet200Response.fromJson(value);
      case 'ApiV1ProductsIdGet200Response':
        return ApiV1ProductsIdGet200Response.fromJson(value);
      case 'ApiV1SubscriptionsGet200Response':
        return ApiV1SubscriptionsGet200Response.fromJson(value);
      case 'ApiV1UsersGet200Response':
        return ApiV1UsersGet200Response.fromJson(value);
      case 'ApiV1WebhooksGet200Response':
        return ApiV1WebhooksGet200Response.fromJson(value);
      case 'ApiV1WebhooksIdGet200Response':
        return ApiV1WebhooksIdGet200Response.fromJson(value);
      case 'ApiV1WebhooksPost201Response':
        return ApiV1WebhooksPost201Response.fromJson(value);
      case 'ApiV1WebhooksPost201ResponseWebhook':
        return ApiV1WebhooksPost201ResponseWebhook.fromJson(value);
      case 'BillingConfig':
        return BillingConfig.fromJson(value);
      case 'BillingPortalRequest':
        return BillingPortalRequest.fromJson(value);
      case 'BillingSubscribe':
        return BillingSubscribe.fromJson(value);
      case 'CreateApiKey':
        return CreateApiKey.fromJson(value);
      case 'CreateInvitation':
        return CreateInvitation.fromJson(value);
      case 'CreateOAuthClient':
        return CreateOAuthClient.fromJson(value);
      case 'CreateOrganization':
        return CreateOrganization.fromJson(value);
      case 'CreateSubscription':
        return CreateSubscription.fromJson(value);
      case 'CreateUser':
        return CreateUser.fromJson(value);
      case 'CreateWebhook':
        return CreateWebhook.fromJson(value);
      case 'Entitlement':
        return Entitlement.fromJson(value);
      case 'Error':
        return Error.fromJson(value);
      case 'Invitation':
        return Invitation.fromJson(value);
      case 'License':
        return License.fromJson(value);
      case 'LicenseCheck':
        return LicenseCheck.fromJson(value);
      case 'OAuthClient':
        return OAuthClient.fromJson(value);
      case 'Organization':
        return Organization.fromJson(value);
      case 'Pagination':
        return Pagination.fromJson(value);
      case 'Product':
        return Product.fromJson(value);
      case 'Subscription':
        return Subscription.fromJson(value);
      case 'Success':
        return Success.fromJson(value);
      case 'UpdateOrganization':
        return UpdateOrganization.fromJson(value);
      case 'UpdateProduct':
        return UpdateProduct.fromJson(value);
      case 'UpdateUser':
        return UpdateUser.fromJson(value);
      case 'UpdateUserImage':
        return UpdateUserImage.fromJson(value);
      case 'UpdateWebhook':
        return UpdateWebhook.fromJson(value);
      case 'User':
        return User.fromJson(value);
      case 'UserMetadata':
        return UserMetadata.fromJson(value);
      case 'Webhook':
        return Webhook.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',);
}