Files
YandexMusic/YandexMusic.API/Models/Common/YExecutionContext.cs
2026-04-10 12:12:33 +03:00

58 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using YandexMusic.API.Common;
namespace YandexMusic.API.Models.Common
{
public sealed class YExecutionContextConverter : JsonConverter
{
#region Поля
private YandexMusicApi api;
private AuthStorage storage;
#endregion Поля
public override bool CanConvert(Type objectType)
{
return typeof(YBaseModel).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
YBaseModel obj = (YBaseModel)Activator.CreateInstance(objectType);
serializer.Populate(reader, obj);
obj.Context = new YExecutionContext
{
API = api,
Storage = storage
};
return obj;
}
catch (Exception ex)
{
throw new Exception($"Ошибка десериализации типа \"{objectType.Name}\".", ex);
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public YExecutionContextConverter(YandexMusicApi yandex, AuthStorage auth)
{
api = yandex;
storage = auth;
}
}
public class YExecutionContext
{
public YandexMusicApi API { get; internal set; }
public AuthStorage Storage { get; internal set; }
}
}