diff --git a/SoundCloudExplode/Playlists/PlaylistClient.cs b/SoundCloudExplode/Playlists/PlaylistClient.cs index 90f8830..2c2bf75 100644 --- a/SoundCloudExplode/Playlists/PlaylistClient.cs +++ b/SoundCloudExplode/Playlists/PlaylistClient.cs @@ -72,7 +72,10 @@ public async ValueTask GetAsync( throw new SoundcloudExplodeException("Invalid playlist url"); var resolvedJson = await endpoint.ResolveUrlAsync(url, cancellationToken); - var playlist = JsonSerializer.Deserialize(resolvedJson)!; + var playlist = JsonSerializer.Deserialize( + resolvedJson, + SourceGenerationContext.Default.Playlist + )!; if (populateAllTracks) { @@ -119,7 +122,10 @@ public async IAsyncEnumerable> GetTrackBatchesAsync( cancellationToken ); - var tracks = JsonSerializer.Deserialize>(response)!; + var tracks = JsonSerializer.Deserialize( + response, + SourceGenerationContext.Default.ListTrack + )!; foreach (var track in tracks) track.PlaylistName = playlist.Title; diff --git a/SoundCloudExplode/Search/SearchClient.cs b/SoundCloudExplode/Search/SearchClient.cs index db159c9..830a97b 100644 --- a/SoundCloudExplode/Search/SearchClient.cs +++ b/SoundCloudExplode/Search/SearchClient.cs @@ -95,7 +95,10 @@ permalinkUrl is null // User result if (permalinkUri.Segments.Length == 2) { - var user = JsonSerializer.Deserialize(item.ToString()!)!; + var user = JsonSerializer.Deserialize( + item.ToString()!, + SourceGenerationContext.Default.UserSearchResult + )!; results.Add(user); continue; } @@ -103,7 +106,10 @@ permalinkUrl is null // Track result if (permalinkUri.Segments.Length == 3) { - var track = JsonSerializer.Deserialize(item.ToString()!)!; + var track = JsonSerializer.Deserialize( + item.ToString()!, + SourceGenerationContext.Default.TrackSearchResult + )!; results.Add(track); continue; } @@ -111,8 +117,9 @@ permalinkUrl is null // Playlist/Album result if (permalinkUri.Segments.Length == 4 && permalinkUri.Segments[2] == "sets/") { - var playlist = JsonSerializer.Deserialize( - item.ToString()! + var playlist = JsonSerializer.Deserialize( + item.ToString()!, + SourceGenerationContext.Default.PlaylistSearchResult )!; results.Add(playlist); } diff --git a/SoundCloudExplode/SourceGenerationContext.cs b/SoundCloudExplode/SourceGenerationContext.cs new file mode 100644 index 0000000..8c665dd --- /dev/null +++ b/SoundCloudExplode/SourceGenerationContext.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; +using SoundCloudExplode.Playlists; +using SoundCloudExplode.Search; +using SoundCloudExplode.Tracks; +using SoundCloudExplode.Users; + +namespace SoundCloudExplode; + +[JsonSerializable(typeof(Track))] +[JsonSerializable(typeof(Playlist))] +[JsonSerializable(typeof(User))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(TrackSearchResult))] +[JsonSerializable(typeof(PlaylistSearchResult))] +[JsonSerializable(typeof(UserSearchResult))] +internal partial class SourceGenerationContext : JsonSerializerContext; diff --git a/SoundCloudExplode/Tracks/TrackClient.cs b/SoundCloudExplode/Tracks/TrackClient.cs index e84e7e7..2ed95f4 100644 --- a/SoundCloudExplode/Tracks/TrackClient.cs +++ b/SoundCloudExplode/Tracks/TrackClient.cs @@ -64,7 +64,7 @@ public async Task IsUrlValidAsync( var resolvedJson = await endpoint.ResolveUrlAsync(url, cancellationToken); - return JsonSerializer.Deserialize(resolvedJson)!; + return JsonSerializer.Deserialize(resolvedJson, SourceGenerationContext.Default.Track)!; } /// @@ -83,7 +83,10 @@ public async Task IsUrlValidAsync( if (trackInfoJson is null) return null; - var track = JsonSerializer.Deserialize(trackInfoJson); + var track = JsonSerializer.Deserialize( + trackInfoJson, + SourceGenerationContext.Default.Track + ); return track is null || track.PermalinkUrl is null ? null : (track.PermalinkUrl?.ToString()); diff --git a/SoundCloudExplode/Users/UserClient.cs b/SoundCloudExplode/Users/UserClient.cs index 0bec2c1..0806e2d 100644 --- a/SoundCloudExplode/Users/UserClient.cs +++ b/SoundCloudExplode/Users/UserClient.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Runtime.CompilerServices; using System.Text.Json; @@ -52,7 +51,7 @@ public async ValueTask GetAsync(string url, CancellationToken cancellation throw new SoundcloudExplodeException("Invalid user url"); var resolvedJson = await endpoint.ResolveUrlAsync(url, cancellationToken); - return JsonSerializer.Deserialize(resolvedJson)!; + return JsonSerializer.Deserialize(resolvedJson, SourceGenerationContext.Default.User)!; } /// @@ -100,8 +99,9 @@ public async IAsyncEnumerable> GetTrackBatchesAsync( break; if ( - JsonSerializer.Deserialize>(collectionStr) is not List list - || !list.Any() + JsonSerializer.Deserialize(collectionStr, SourceGenerationContext.Default.ListTrack) + is not List list + || list.Count == 0 ) { break; @@ -204,8 +204,12 @@ public async IAsyncEnumerable> GetPlaylistBatchesAsync( break; if ( - JsonSerializer.Deserialize>(collectionStr) is not List list - || !list.Any() + JsonSerializer.Deserialize( + collectionStr, + SourceGenerationContext.Default.ListPlaylist + ) + is not List list + || list.Count == 0 ) { break; @@ -280,8 +284,12 @@ public async IAsyncEnumerable> GetAlbumBatchesAsync( break; if ( - JsonSerializer.Deserialize>(collectionStr) is not List list - || !list.Any() + JsonSerializer.Deserialize( + collectionStr, + SourceGenerationContext.Default.ListPlaylist + ) + is not List list + || list.Count == 0 ) { break;