Обнновлено до .net10
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user