Skip to content

FullPlaylist serialization and deserialization with Newtonsoft.Json throws exception #604

Description

@herterjo

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)`'

This is probably due to https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Models/Converters/PlayableItemConverter.cs not having a usable WriteJson method.

Tested on .Net Core 2.1 with SpotifyAPI.Web from Nuget with version 6.2.0

Demo code (replace "clientId" and "clientSecret"):

using Newtonsoft.Json;
using SpotifyAPI.Web;

namespace Spotify_Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            SpotifyClientConfig clientConfig = SpotifyClientConfig
                .CreateDefault()
                .WithAuthenticator(new ClientCredentialsAuthenticator("clientId", "clientSecret"));
            SpotifyClient client = new SpotifyClient(clientConfig);
            FullPlaylist playlist = client.Playlists.Get("37i9dQZF1DWZUTt0fNaCPB").Result;
            string json = JsonConvert.SerializeObject(playlist);
            // Exception is thrown in following method:
            playlist = JsonConvert.DeserializeObject<FullPlaylist>(json);
        }
    }
}

Activity

  1. JohnnyCrazy commented on Apr 30, 2021

    @JohnnyCrazy
    Owner

    Hi,

    Interesting find!

    1. 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...
    2. 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 👍

  2. JohnnyCrazy commented on May 1, 2021

    @JohnnyCrazy
    Owner

    Decided to go for the first fix, we ignore the case when getting the field value. Should now be fixed in the master @herterjo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions