Обнновлено до .net10
This commit is contained in:
@@ -1,54 +1,8 @@
|
||||
namespace YandexMusic.API.Models.Radio.Restriction
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
|
||||
public class YRestriction
|
||||
{
|
||||
public sealed class YRestrictionConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return typeof(YRestriction).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);
|
||||
YRestriction restriction;
|
||||
|
||||
try
|
||||
{
|
||||
YRestrictionType type = jObject["type"].ToObject<YRestrictionType>();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case YRestrictionType.Enum:
|
||||
restriction = jObject.ToObject<YRestrictionEnum>();
|
||||
break;
|
||||
case YRestrictionType.DiscreteScale:
|
||||
restriction = jObject.ToObject<YRestrictionDiscreteScale>();
|
||||
break;
|
||||
default:
|
||||
restriction = jObject.ToObject<YRestriction>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Ошибка десериализации типа \"{objectType.Name}\".", ex);
|
||||
}
|
||||
|
||||
return restriction;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class YRestriction
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public YRestrictionType Type { get; set; }
|
||||
}
|
||||
public string Name { get; set; }
|
||||
public YRestrictionType Type { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
/// <summary>Конвертер для ограничений радиостанции (дискретная шкала или перечисление).</summary>
|
||||
public class YRestrictionConverter : JsonConverter<YRestriction>
|
||||
{
|
||||
public override YRestriction? 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 typeProp = root.GetProperty("type");
|
||||
var typeStr = typeProp.GetString();
|
||||
if (!Enum.TryParse<YRestrictionType>(typeStr, true, out var restrictionType))
|
||||
throw new JsonException($"Неизвестный тип ограничения: {typeStr}");
|
||||
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
return restrictionType switch
|
||||
{
|
||||
YRestrictionType.Enum => JsonSerializer.Deserialize<YRestrictionEnum>(rawText, options),
|
||||
YRestrictionType.DiscreteScale => JsonSerializer.Deserialize<YRestrictionDiscreteScale>(rawText, options),
|
||||
_ => JsonSerializer.Deserialize<YRestriction>(rawText, options)
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, YRestriction value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace YandexMusic.API.Models.Radio.Restriction
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
public class YRestrictionDiscreteScale : YRestriction
|
||||
{
|
||||
public class YRestrictionDiscreteScale : YRestriction
|
||||
{
|
||||
public YRestrictionValue<int> Min { get; set; }
|
||||
public YRestrictionValue<int> Max { get; set; }
|
||||
}
|
||||
public YRestrictionValue<int> Min { get; set; }
|
||||
public YRestrictionValue<int> Max { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Radio.Restriction
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
public class YRestrictionEnum : YRestriction
|
||||
{
|
||||
public class YRestrictionEnum : YRestriction
|
||||
{
|
||||
public List<YRestrictionValue<string>> PossibleValues { get; set; }
|
||||
}
|
||||
public List<YRestrictionValue<string>> PossibleValues { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Radio.Restriction
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum YRestrictionType
|
||||
{
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum YRestrictionType
|
||||
{
|
||||
[EnumMember(Value = "discrete-scale")]
|
||||
DiscreteScale,
|
||||
Enum
|
||||
}
|
||||
[EnumMember(Value = "discrete-scale")]
|
||||
DiscreteScale,
|
||||
Enum
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace YandexMusic.API.Models.Radio.Restriction
|
||||
namespace YandexMusic.API.Models.Radio.Restriction;
|
||||
|
||||
public class YRestrictionValue<T>
|
||||
{
|
||||
public class YRestrictionValue<T>
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public T Value { get; set; }
|
||||
}
|
||||
public string Name { get; set; }
|
||||
public T Value { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user