2020-05-01 19:05:28 +01:00
|
|
|
using System;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
2020-05-07 17:03:20 +01:00
|
|
|
public class PlayableItemConverter : JsonConverter
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2020-06-16 19:02:58 +01:00
|
|
|
public override bool CanWrite { get => false; }
|
|
|
|
|
2020-05-01 19:05:28 +01:00
|
|
|
public override bool CanConvert(Type objectType) => true;
|
|
|
|
|
2020-05-25 17:00:38 +01:00
|
|
|
public override object? ReadJson(JsonReader reader, Type objectType,
|
|
|
|
object? existingValue, JsonSerializer serializer)
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
Ensure.ArgumentNotNull(serializer, nameof(serializer));
|
|
|
|
|
2021-05-01 16:11:24 +01:00
|
|
|
if (JToken.ReadFrom(reader) is not JObject obj)
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-01 16:11:24 +01:00
|
|
|
var type = obj.GetValue("type", StringComparison.OrdinalIgnoreCase)?.Value<string>();
|
|
|
|
if (string.Equals(type, "track", StringComparison.OrdinalIgnoreCase))
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2021-05-01 16:11:24 +01:00
|
|
|
var track = new FullTrack();
|
|
|
|
serializer.Populate(obj.CreateReader(), track);
|
|
|
|
return track;
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
2021-05-01 16:11:24 +01:00
|
|
|
else if (string.Equals(type, "episode", StringComparison.OrdinalIgnoreCase))
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2021-05-01 16:11:24 +01:00
|
|
|
var episode = new FullEpisode();
|
|
|
|
serializer.Populate(obj.CreateReader(), episode);
|
|
|
|
return episode;
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-26 17:28:44 +00:00
|
|
|
throw new APIException($@"Received unkown playlist element type: {type}.
|
2020-06-29 21:52:15 +01:00
|
|
|
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)`");
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:00:38 +01:00
|
|
|
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
|
|
|
throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 17:00:38 +01:00
|
|
|
|