Esempio n. 1
0
        public override Show Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is not JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var copyrightArrayConverter          = options.GetConverter <CopyrightArray>();
            var countryCodeArrayConverter        = options.GetConverter <CountryCodeArray>();
            var externalUrlsConverter            = options.GetConverter <ExternalUrls>();
            var imageArrayConverter              = options.GetConverter <ImageArray>();
            var simplifiedEpisodePagingConverter = options.GetConverter <Paging <SimplifiedEpisode> >();
            var stringArrayConverter             = options.GetConverter <StringArray>();
            var uriConverter = options.GetConverter <Uri>();

            string     id          = string.Empty;
            Uri        uri         = null !;
            Uri        href        = null !;
            string     name        = string.Empty;
            string     description = string.Empty;
            ImageArray images      = Array.Empty <Image>();
            Paging <SimplifiedEpisode> episodes = null !;
            bool             isExplicit         = default;
            StringArray      languages          = Array.Empty <string>();
            CountryCodeArray availableMarkets   = Array.Empty <CountryCode>();
            string           mediaType          = string.Empty;
            string           publisher          = string.Empty;
            CopyrightArray   copyrights         = Array.Empty <Copyright>();
            bool?            isExternallyHosted = null;
            ExternalUrls     externalUrls       = null !;

            while (reader.Read())
            {
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType is not JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                var propertyName = reader.GetString();

                reader.Read(); // Read to next token.

                switch (propertyName)
                {
                case "id":
                    id = reader.GetString() !;
                    break;

                case "uri":
                    uri = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "href":
                    href = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "name":
                    name = reader.GetString() !;
                    break;

                case "description":
                    description = reader.GetString() !;
                    break;

                case "images":
                    images = imageArrayConverter.Read(ref reader, typeof(ImageArray), options) !;
                    break;

                case "episodes":
                    episodes = simplifiedEpisodePagingConverter.Read(ref reader, typeof(Paging <SimplifiedEpisode>), options) !;
                    break;

                case "explicit":
                    isExplicit = reader.GetBoolean();
                    break;

                case "is_externally_hosted":
                    isExternallyHosted = reader.TokenType switch
                    {
                        JsonTokenType.Null => null,
                        JsonTokenType.True => true,
                        JsonTokenType.False => false,
                        _ => throw new JsonException()
                    };
                    break;

                case "languages":
                    languages = stringArrayConverter.Read(ref reader, typeof(StringArray), options) !;
                    break;

                case "available_markets":
                    availableMarkets = countryCodeArrayConverter.Read(ref reader, typeof(CountryCodeArray), options) !;
                    break;

                case "media_type":
                    mediaType = reader.GetString() !;
                    break;

                case "publisher":
                    publisher = reader.GetString() !;
                    break;

                case "copyrights":
                    copyrights = copyrightArrayConverter.Read(ref reader, typeof(CopyrightArray), options) !;
                    break;

                case "external_urls":
                    externalUrls = externalUrlsConverter.Read(ref reader, typeof(ExternalUrls), options) !;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new(
                       id,
                       uri,
                       href,
                       name,
                       description,
                       images,
                       episodes,
                       isExplicit,
                       isExternallyHosted,
                       languages,
                       availableMarkets,
                       mediaType,
                       publisher,
                       copyrights,
                       externalUrls));
        }
        public override Album Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is not JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var copyrightArrayConverter        = options.GetConverter <CopyrightArray>();
            var countryCodeArrayConverter      = options.GetConverter <CountryCodeArray>();
            var externalIdsConverter           = options.GetConverter <ExternalIds>();
            var externalUrlsConverter          = options.GetConverter <ExternalUrls>();
            var imageArrayConverter            = options.GetConverter <ImageArray>();
            var simplifiedArtistArrayConverter = options.GetConverter <SimplifiedArtistArray>();
            var simplifiedTrackPagingConverter = options.GetConverter <Paging <SimplifiedTrack> >();
            var stringArrayConverter           = options.GetConverter <StringArray>();
            var uriConverter = options.GetConverter <Uri>();

            string                   id                   = string.Empty;
            Uri                      uri                  = null !;
            Uri                      href                 = null !;
            string                   name                 = string.Empty;
            AlbumType                type                 = default;
            ImageArray               images               = Array.Empty <Image>();
            SimplifiedArtistArray    artists              = Array.Empty <SimplifiedArtist>();
            DateTime                 releaseDate          = default;
            ReleaseDatePrecision     releaseDatePrecision = default;
            CountryCodeArray         availableMarkets     = Array.Empty <CountryCode>();
            CopyrightArray           copyrights           = Array.Empty <Copyright>();
            ExternalIds              externalIds          = null !;
            ExternalUrls             externalUrls         = null !;
            StringArray              genres               = Array.Empty <string>();
            string                   label                = string.Empty;
            int                      popularity           = default;
            Paging <SimplifiedTrack> tracks               = null !;

            while (reader.Read())
            {
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType is not JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                var propertyName = reader.GetString();

                reader.Read(); // Read to next token.

                switch (propertyName)
                {
                case "id":
                    id = reader.GetString() !;
                    break;

                case "uri":
                    uri = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "href":
                    href = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "name":
                    name = reader.GetString() !;
                    break;

                case "album_type":
                    type = AlbumTypeConverter.FromSpotifyString(reader.GetString() !);
                    break;

                case "images":
                    images = imageArrayConverter.Read(ref reader, typeof(ImageArray), options) !;
                    break;

                case "artists":
                    artists = simplifiedArtistArrayConverter.Read(ref reader, typeof(SimplifiedArtistArray), options) !;
                    break;

                case "release_date":
                    releaseDate = reader.GetReleaseDate();
                    break;

                case "release_date_precision":
                    releaseDatePrecision = ReleaseDatePrecisionConverter.FromSpotifyString(reader.GetString() !);
                    break;

                case "tracks":
                    tracks = simplifiedTrackPagingConverter.Read(ref reader, typeof(Paging <SimplifiedTrack>), options) !;
                    break;

                case "genres":
                    genres = stringArrayConverter.Read(ref reader, typeof(StringArray), options) !;
                    break;

                case "popularity":
                    popularity = reader.GetInt32();
                    break;

                case "available_markets":
                    availableMarkets = countryCodeArrayConverter.Read(ref reader, typeof(CountryCodeArray), options) !;
                    break;

                case "label":
                    label = reader.GetString() !;
                    break;

                case "copyrights":
                    copyrights = copyrightArrayConverter.Read(ref reader, typeof(CopyrightArray), options) !;
                    break;

                case "external_ids":
                    externalIds = externalIdsConverter.Read(ref reader, typeof(ExternalIds), options) !;
                    break;

                case "external_urls":
                    externalUrls = externalUrlsConverter.Read(ref reader, typeof(ExternalUrls), options) !;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new(
                       id,
                       uri,
                       href,
                       name,
                       type,
                       images,
                       artists,
                       releaseDate,
                       releaseDatePrecision,
                       tracks,
                       genres,
                       popularity,
                       availableMarkets,
                       label,
                       copyrights,
                       externalIds,
                       externalUrls));
        }
        public override Track Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is not JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var countryCodeArrayConverter      = options.GetConverter <CountryCodeArray>();
            var externalIdsConverter           = options.GetConverter <ExternalIds>();
            var externalUrlsConverter          = options.GetConverter <ExternalUrls>();
            var simplifiedAlbumConverter       = options.GetConverter <SimplifiedAlbum>();
            var simplifiedArtistArrayConverter = options.GetConverter <SimplifiedArtistArray>();
            var uriConverter = options.GetConverter <Uri>();

            string                id               = string.Empty;
            Uri                   uri              = null !;
            Uri                   href             = null !;
            string                name             = string.Empty;
            SimplifiedAlbum       album            = null !;
            SimplifiedArtistArray artists          = Array.Empty <SimplifiedArtist>();
            int                   duration         = default;
            int                   trackNumber      = default;
            int                   discNumber       = default;
            bool                  isExplicit       = default;
            bool                  isLocal          = default;
            CountryCodeArray      availableMarkets = Array.Empty <CountryCode>();
            int                   popularity       = default;
            string                previewUrl       = string.Empty;
            ExternalIds           externalIds      = null !;
            ExternalUrls          externalUrls     = null !;

            while (reader.Read())
            {
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType is not JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                var propertyName = reader.GetString();

                reader.Read(); // Read to next token.

                switch (propertyName)
                {
                case "id":
                    id = reader.GetString() !;
                    break;

                case "uri":
                    uri = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "href":
                    href = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "name":
                    name = reader.GetString() !;
                    break;

                case "album":
                    album = simplifiedAlbumConverter.Read(ref reader, typeof(SimplifiedAlbum), options) !;
                    break;

                case "artists":
                    artists = simplifiedArtistArrayConverter.Read(ref reader, typeof(SimplifiedArtistArray), options) !;
                    break;

                case "duration_ms":
                    duration = reader.GetInt32();
                    break;

                case "track_number":
                    trackNumber = reader.GetInt32();
                    break;

                case "disc_number":
                    discNumber = reader.GetInt32();
                    break;

                case "explicit":
                    isExplicit = reader.GetBoolean();
                    break;

                case "is_local":
                    isLocal = reader.GetBoolean();
                    break;

                case "available_markets":
                    availableMarkets = countryCodeArrayConverter.Read(ref reader, typeof(CountryCodeArray), options) !;
                    break;

                case "popularity":
                    popularity = reader.GetInt32();
                    break;

                case "preview_url":
                    previewUrl = reader.GetString() !;
                    break;

                case "external_ids":
                    externalIds = externalIdsConverter.Read(ref reader, typeof(ExternalIds), options) !;
                    break;

                case "external_urls":
                    externalUrls = externalUrlsConverter.Read(ref reader, typeof(ExternalUrls), options) !;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new(
                       id,
                       uri,
                       href,
                       name,
                       album,
                       artists,
                       duration,
                       discNumber,
                       trackNumber,
                       isExplicit,
                       isLocal,
                       availableMarkets,
                       popularity,
                       previewUrl,
                       externalIds,
                       externalUrls));
        }
        public override SimplifiedAlbum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is not JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var countryCodeArrayConverter      = options.GetConverter <CountryCodeArray>();
            var externalUrlsConverter          = options.GetConverter <ExternalUrls>();
            var imageArrayConverter            = options.GetConverter <ImageArray>();
            var simplifiedArtistArrayConverter = options.GetConverter <SimplifiedArtistArray>();
            var uriConverter = options.GetConverter <Uri>();

            string                id                   = string.Empty;
            Uri                   uri                  = null !;
            Uri                   href                 = null !;
            string                name                 = string.Empty;
            AlbumType             type                 = default;
            AlbumGroups?          group                = null;
            ImageArray            images               = null !;
            SimplifiedArtistArray artists              = null !;
            DateTime              releaseDate          = default;
            ReleaseDatePrecision  releaseDatePrecision = default;
            CountryCodeArray      availableMarkets     = null !;
            ExternalUrls          externalUrls         = null !;

            while (reader.Read())
            {
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType is not JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                var propertyName = reader.GetString();

                reader.Read(); // Read to next token.

                switch (propertyName)
                {
                case "id":
                    id = reader.GetString() !;
                    break;

                case "uri":
                    uri = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "href":
                    href = uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

                case "name":
                    name = reader.GetString() !;
                    break;

                case "album_type":
                    type = AlbumTypeConverter.FromSpotifyString(reader.GetString() !.ToLower());
                    break;

                case "album_group":
                    group = AlbumGroupConverter.FromSpotifyString(reader.GetString() !.ToLower());
                    break;

                case "images":
                    images = imageArrayConverter.Read(ref reader, typeof(ImageArray), options) !;
                    break;

                case "artists":
                    artists = simplifiedArtistArrayConverter.Read(ref reader, typeof(SimplifiedArtistArray), options) !;
                    break;

                case "release_date":
                    var date = reader.GetString() !.Split('-');
                    releaseDate = new(
                        date.Length > 0 ? int.Parse(date[0]) : 1,
                        date.Length > 1 ? int.Parse(date[1]) : 1,
                        date.Length > 2 ? int.Parse(date[2]) : 1);
                    break;

                case "release_date_precision":
                    releaseDatePrecision = ReleaseDatePrecisionConverter.FromSpotifyString(reader.GetString() !);
                    break;

                case "available_markets":
                    availableMarkets = countryCodeArrayConverter.Read(ref reader, typeof(CountryCodeArray), options) !;
                    break;

                case "external_urls":
                    externalUrls = externalUrlsConverter.Read(ref reader, typeof(ExternalUrls), options) !;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new(
                       id,
                       uri,
                       href,
                       name,
                       type,
                       group,
                       images,
                       artists,
                       releaseDate,
                       releaseDatePrecision,
                       availableMarkets,
                       externalUrls));
        }