Обнновлено до .net10
This commit is contained in:
@@ -1,58 +1,8 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
public class YPlayContext
|
||||
{
|
||||
public sealed class YPlayContextConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return typeof(YLandingEntity).IsAssignableFrom(objectType);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
|
||||
JObject jObject = JObject.Load(reader);
|
||||
|
||||
YPlayContext context;
|
||||
try
|
||||
{
|
||||
YPlayContextType type = jObject["context"].ToObject<YPlayContextType>();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case YPlayContextType.Album:
|
||||
context = jObject.ToObject<YPlayContextAlbum>();
|
||||
break;
|
||||
case YPlayContextType.Artist:
|
||||
context = jObject.ToObject<YPlayContextArtist>();
|
||||
break;
|
||||
case YPlayContextType.Playlist:
|
||||
context = jObject.ToObject<YPlayContextPlaylist>();
|
||||
break;
|
||||
default:
|
||||
context = jObject.ToObject<YPlayContext>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Ошибка десериализации типа \"{jObject["type"]}\".", ex);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class YPlayContext
|
||||
{
|
||||
public string Client { get; set; }
|
||||
public YPlayContextType Context { get; set; }
|
||||
public string ContextItem { get; set; }
|
||||
}
|
||||
public string Client { get; set; }
|
||||
public YPlayContextType Context { get; set; }
|
||||
public string ContextItem { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using YandexMusic.API.Models.Album;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
public class YPlayContextAlbum : YPlayContext
|
||||
{
|
||||
public class YPlayContextAlbum : YPlayContext
|
||||
{
|
||||
public YAlbum Payload { get; set; }
|
||||
}
|
||||
public YAlbum Payload { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using YandexMusic.API.Models.Artist;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
public class YPlayContextArtist : YPlayContext
|
||||
{
|
||||
public class YPlayContextArtist : YPlayContext
|
||||
{
|
||||
public YArtist Payload { get; set; }
|
||||
}
|
||||
public YArtist Payload { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
/// <summary>Конвертер для контекста воспроизведения (альбом, исполнитель, плейлист).</summary>
|
||||
public class YPlayContextConverter : JsonConverter<YPlayContext>
|
||||
{
|
||||
public override YPlayContext? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartObject)
|
||||
throw new JsonException("Ожидается объект контекста");
|
||||
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
var root = doc.RootElement;
|
||||
|
||||
var contextProp = root.GetProperty("context");
|
||||
var contextStr = contextProp.GetString();
|
||||
if (!Enum.TryParse<YPlayContextType>(contextStr, true, out var contextType))
|
||||
throw new JsonException($"Неизвестный тип контекста: {contextStr}");
|
||||
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
return contextType switch
|
||||
{
|
||||
YPlayContextType.Album => JsonSerializer.Deserialize<YPlayContextAlbum>(rawText, options),
|
||||
YPlayContextType.Artist => JsonSerializer.Deserialize<YPlayContextArtist>(rawText, options),
|
||||
YPlayContextType.Playlist => JsonSerializer.Deserialize<YPlayContextPlaylist>(rawText, options),
|
||||
_ => JsonSerializer.Deserialize<YPlayContext>(rawText, options)
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, YPlayContext value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using YandexMusic.API.Models.Playlist;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
public class YPlayContextPlaylist : YPlayContext
|
||||
{
|
||||
public class YPlayContextPlaylist : YPlayContext
|
||||
{
|
||||
public YPlaylist Payload { get; set; }
|
||||
}
|
||||
public YPlaylist Payload { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
public enum YPlayContextType
|
||||
{
|
||||
public enum YPlayContextType
|
||||
{
|
||||
Album,
|
||||
Artist,
|
||||
Playlist
|
||||
}
|
||||
Album,
|
||||
Artist,
|
||||
Playlist,
|
||||
}
|
||||
Reference in New Issue
Block a user