You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FullPlaylist serialization and deserialization with Newtonsoft.Json throws exception #604
When serializing and deserializing a FullPlaylist with Newtonsoft.Json the following exception is thrown:
SpotifyAPI.Web.APIException: 'Received unkown playlist element type: .
If you're requesting a subset of available fields via the fields query paramter,
make sure to include at least the type field. Often it's `items(track(type))` or `item(type)`'
Tested on .Net Core 2.1 with SpotifyAPI.Web from Nuget with version 6.2.0
Demo code (replace "clientId" and "clientSecret"):
usingNewtonsoft.Json;usingSpotifyAPI.Web;namespaceSpotify_Demo{internalclassProgram{privatestaticvoidMain(string[]args){SpotifyClientConfigclientConfig=SpotifyClientConfig.CreateDefault().WithAuthenticator(newClientCredentialsAuthenticator("clientId","clientSecret"));SpotifyClientclient=newSpotifyClient(clientConfig);FullPlaylistplaylist=client.Playlists.Get("37i9dQZF1DWZUTt0fNaCPB").Result;stringjson=JsonConvert.SerializeObject(playlist);// Exception is thrown in following method:playlist=JsonConvert.DeserializeObject<FullPlaylist>(json);}}}
The exception is actually thrown becase it tries to find the type field in a PlaylistTrack object. But since JsonConvert.SerializeObject with default settings is called before, the field is now Type because it follows C# naming conventions! I'm not quite sure what we should do here:
We could either get the values for type and Type and see which is filled. Not a real fan of that solution
Ignore the error and expect people to use lowercase SerializeObject. Also not a fan...
Why does JsonConvert.SerializeObject(playlist) even work...According to NewtonsoftJSON docs:
Gets a value indicating whether this JsonConverter can write JSON
So IMO it should fail here already.
I will investigate some more and see with what solution we can come up with. Happy for some input tho 👍
Activity
JohnnyCrazy commentedon Apr 30, 2021
Hi,
Interesting find!
The exception is actually thrown becase it tries to find the
typefield in aPlaylistTrackobject. But sinceJsonConvert.SerializeObjectwith default settings is called before, the field is nowTypebecause it follows C# naming conventions! I'm not quite sure what we should do here:typeandTypeand see which is filled. Not a real fan of that solutionSerializeObject. Also not a fan...Why does
JsonConvert.SerializeObject(playlist)even work...According to NewtonsoftJSON docs:I will investigate some more and see with what solution we can come up with. Happy for some input tho 👍
JohnnyCrazy commentedon May 1, 2021
Decided to go for the first fix, we ignore the case when getting the field value. Should now be fixed in the master @herterjo !