コード例 #1
0
        public override SimplifiedEpisode Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType is not JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var externalUrlsConverter = options.GetConverter <ExternalUrls>();
            var imageArrayConverter   = options.GetConverter <ImageArray>();
            var resumePointConverter  = options.GetConverter <ResumePoint>();
            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>();
            int                  duration             = default;
            DateTime             releaseDate          = default;
            ReleaseDatePrecision releaseDatePrecision = default;
            bool                 isExplicit           = default;
            bool                 isPlayable           = default;
            bool                 isExternallyHosted   = default;
            StringArray          languages            = Array.Empty <string>();
            Uri?                 audioPreviewUrl      = null;
            ExternalUrls         externalUrls         = null !;
            ResumePoint?         resumePoint          = 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 "duration_ms":
                    duration = reader.GetInt32();
                    break;

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

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

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

                case "is_playable":
                    isPlayable = reader.GetBoolean();
                    break;

                case "is_externally_hosted":
                    isExternallyHosted = reader.GetBoolean();
                    break;

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

                case "audio_preview_url":
                    audioPreviewUrl = (reader.TokenType is JsonTokenType.Null) ? null : uriConverter.Read(ref reader, typeof(Uri), options) !;
                    break;

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

                case "resume_point":
                    resumePoint = resumePointConverter.Read(ref reader, typeof(ResumePoint), options) !;
                    break;

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

            return(new(
                       id,
                       uri,
                       href,
                       name,
                       description,
                       images,
                       duration,
                       releaseDate,
                       releaseDatePrecision,
                       isExplicit,
                       isPlayable,
                       isExternallyHosted,
                       languages,
                       audioPreviewUrl,
                       externalUrls,
                       resumePoint));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        public static void WriteReleaseDate(this Utf8JsonWriter writer, DateTime releaseDate, ReleaseDatePrecision releaseDatePrecision)
        {
            var stringValue = releaseDatePrecision switch
            {
                ReleaseDatePrecision.Year => $"{releaseDate.Year}",
                ReleaseDatePrecision.Month => $"{releaseDate.Year}-{releaseDate.Month}",
                ReleaseDatePrecision.Day => $"{releaseDate.Year}-{releaseDate.Month}-{releaseDate.Day}",
                _ => releaseDate.ToString(CultureInfo.InvariantCulture)
            };

            writer.WriteString("release_date", stringValue);
        }
    }
コード例 #4
0
        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));
        }
コード例 #5
0
 public static string ToSpotifyString(this ReleaseDatePrecision releaseDatePrecision) => releaseDatePrecision switch
 {