Обнновлено до .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,
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YAlbumMenuItem : YBlockEntity
|
||||
{
|
||||
public class YAlbumMenuItem : YBlockEntity
|
||||
{
|
||||
public string Title { get; set; }
|
||||
}
|
||||
public string Title { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YBlockEntity
|
||||
{
|
||||
public class YBlockEntity
|
||||
{
|
||||
public string BlockEntityDataId { get; set; }
|
||||
}
|
||||
public string BlockEntityDataId { get; set; }
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
{
|
||||
public class YCategory : YBlockEntity
|
||||
{
|
||||
public string BackgroundImageUri { get; set; }
|
||||
public string CategoryId { get; set; }
|
||||
public bool HasBackgroundImageText { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string UrlScheme { get; set; }
|
||||
public string VoiceTitle { get; set; }
|
||||
public string TextColor { get; set; }
|
||||
public string TextBackgroundColor { get; set; }
|
||||
public string Title { get; set; }
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YCategory : YBlockEntity
|
||||
{
|
||||
public string BackgroundImageUri { get; set; }
|
||||
public string CategoryId { get; set; }
|
||||
public bool HasBackgroundImageText { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string UrlScheme { get; set; }
|
||||
public string VoiceTitle { get; set; }
|
||||
public string TextColor { get; set; }
|
||||
public string TextBackgroundColor { get; set; }
|
||||
public string Title { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
using YandexMusic.API.Models.Common;
|
||||
using YandexMusic.API.Models.Track;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YChartItem
|
||||
{
|
||||
public class YChartItem
|
||||
{
|
||||
public YTrack Track { get; set; }
|
||||
public YChart Chart { get; set; }
|
||||
}
|
||||
public YTrack Track { get; set; }
|
||||
public YChart Chart { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public enum YChartProgress
|
||||
{
|
||||
public enum YChartProgress
|
||||
{
|
||||
New,
|
||||
Up,
|
||||
Down,
|
||||
Same
|
||||
}
|
||||
New,
|
||||
Up,
|
||||
Down,
|
||||
Same,
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YClientWidget : YBlockEntity
|
||||
{
|
||||
public class YClientWidget : YBlockEntity
|
||||
{
|
||||
public string ImagePath { get; set; }
|
||||
public string Subtitle { get; set; }
|
||||
public string Text { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Type { get; set; }
|
||||
}
|
||||
public string ImagePath { get; set; }
|
||||
public string Subtitle { get; set; }
|
||||
public string Text { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Type { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using YandexMusic.API.Models.Album;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityAlbum : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityAlbum : YLandingEntity
|
||||
{
|
||||
public YAlbum Data { get; set; }
|
||||
}
|
||||
public YAlbum Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityAlbumMenuItem : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityAlbumMenuItem : YLandingEntity
|
||||
{
|
||||
public YAlbumMenuItem Data { get; set; }
|
||||
}
|
||||
public YAlbumMenuItem Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityCategory : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityCategory : YLandingEntity
|
||||
{
|
||||
public YCategory Data { get; set; }
|
||||
}
|
||||
public YCategory Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityChart : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityChart : YLandingEntity
|
||||
{
|
||||
public YChartItem Data { get; set; }
|
||||
}
|
||||
public YChartItem Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityClientWidget : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityClientWidget : YLandingEntity
|
||||
{
|
||||
public YClientWidget Data { get; set; }
|
||||
}
|
||||
public YClientWidget Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityPersonalPlaylist : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityPersonalPlaylist : YLandingEntity
|
||||
{
|
||||
public YPersonalPlaylist Data { get; set; }
|
||||
}
|
||||
public YPersonalPlaylist Data { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
using YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
using System.Text.Json.Serialization;
|
||||
using YandexMusic.API.Models.Landing.Entity.Entities.Context;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityPlayContext : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityPlayContext : YLandingEntity
|
||||
{
|
||||
[JsonConverter(typeof(YPlayContextConverter))]
|
||||
public YPlayContext Data { get; set; }
|
||||
}
|
||||
[JsonConverter(typeof(YPlayContextConverter))]
|
||||
public YPlayContext Data { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using YandexMusic.API.Models.Playlist;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityPlaylist : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityPlaylist : YLandingEntity
|
||||
{
|
||||
public YPlaylist Data { get; set; }
|
||||
}
|
||||
public YPlaylist Data { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityPodcast : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityPodcast : YLandingEntity
|
||||
{
|
||||
public string Description { get; set; }
|
||||
public string DescriptionFormatted { get; set; }
|
||||
public DateTime LastUpdated { get; set; }
|
||||
public YPodcast Data { get; set; }
|
||||
}
|
||||
public string Description { get; set; }
|
||||
public string DescriptionFormatted { get; set; }
|
||||
public DateTime LastUpdated { get; set; }
|
||||
public YPodcast Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityPromotion : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityPromotion : YLandingEntity
|
||||
{
|
||||
public YPromotion Data { get; set; }
|
||||
}
|
||||
public YPromotion Data { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingEntityStation : YLandingEntity
|
||||
{
|
||||
public class YLandingEntityStation : YLandingEntity
|
||||
{
|
||||
public YCategory Data { get; set; }
|
||||
}
|
||||
public YCategory Data { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
using YandexMusic.API.Models.Radio;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YLandingStation
|
||||
{
|
||||
public class YLandingStation
|
||||
{
|
||||
public YStationId Id { get; set; }
|
||||
public YStationDescription Data { get; set; }
|
||||
}
|
||||
public YStationId Id { get; set; }
|
||||
public YStationDescription Data { get; set; }
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
using YandexMusic.API.Models.Playlist;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YPersonalPlaylist
|
||||
{
|
||||
public class YPersonalPlaylist
|
||||
{
|
||||
public YPlaylist Data { get; set; }
|
||||
public List<string> Description { get; set; }
|
||||
public bool Notify { get; set; }
|
||||
public string PreviewDescription { get; set; }
|
||||
public bool Ready { get; set; }
|
||||
public string Type { get; set; }
|
||||
}
|
||||
public YPlaylist Data { get; set; }
|
||||
public List<string> Description { get; set; }
|
||||
public bool Notify { get; set; }
|
||||
public string PreviewDescription { get; set; }
|
||||
public bool Ready { get; set; }
|
||||
public string Type { get; set; }
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using YandexMusic.API.Models.Album;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YPodcast
|
||||
{
|
||||
public class YPodcast
|
||||
{
|
||||
public string Description { get; set; }
|
||||
public string DescriptionFormatted { get; set; }
|
||||
public DateTime LastUpdated { get; set; }
|
||||
public YAlbum Podcast { get; set; }
|
||||
}
|
||||
public string Description { get; set; }
|
||||
public string DescriptionFormatted { get; set; }
|
||||
public DateTime LastUpdated { get; set; }
|
||||
public YAlbum Podcast { get; set; }
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities
|
||||
namespace YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
public class YPromotion
|
||||
{
|
||||
public class YPromotion
|
||||
{
|
||||
public string PromoId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Subtitle { get; set; }
|
||||
public string Heading { get; set; }
|
||||
public string UrlScheme { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string TextColor { get; set; }
|
||||
public string Gradient { get; set; }
|
||||
public string Image { get; set; }
|
||||
}
|
||||
public string PromoId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Subtitle { get; set; }
|
||||
public string Heading { get; set; }
|
||||
public string UrlScheme { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string TextColor { get; set; }
|
||||
public string Gradient { get; set; }
|
||||
public string Image { get; set; }
|
||||
}
|
||||
@@ -1,90 +1,7 @@
|
||||
using YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
namespace YandexMusic.API.Models.Landing.Entity;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity
|
||||
public abstract class YLandingEntity
|
||||
{
|
||||
public sealed class YLandingEntityConverter : JsonConverter
|
||||
{
|
||||
private YLandingEntity GetEntity(JToken jObject)
|
||||
{
|
||||
YLandingEntity entity;
|
||||
|
||||
try
|
||||
{
|
||||
YLandingEntityType type = jObject["type"].ToObject<YLandingEntityType>();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case YLandingEntityType.Album:
|
||||
entity = jObject.ToObject<YLandingEntityAlbum>();
|
||||
break;
|
||||
case YLandingEntityType.ChartItem:
|
||||
entity = jObject.ToObject<YLandingEntityChart>();
|
||||
break;
|
||||
case YLandingEntityType.PersonalPlaylist:
|
||||
entity = jObject.ToObject<YLandingEntityPersonalPlaylist>();
|
||||
break;
|
||||
case YLandingEntityType.PlayContext:
|
||||
entity = jObject.ToObject<YLandingEntityPlayContext>();
|
||||
break;
|
||||
case YLandingEntityType.Playlist:
|
||||
entity = jObject.ToObject<YLandingEntityPlaylist>();
|
||||
break;
|
||||
case YLandingEntityType.Podcast:
|
||||
entity = jObject.ToObject<YLandingEntityPodcast>();
|
||||
break;
|
||||
case YLandingEntityType.Promotion:
|
||||
entity = jObject.ToObject<YLandingEntityPromotion>();
|
||||
break;
|
||||
case YLandingEntityType.Category:
|
||||
entity = jObject.ToObject<YLandingEntityCategory>();
|
||||
break;
|
||||
case YLandingEntityType.Station:
|
||||
entity = jObject.ToObject<YLandingEntityStation>();
|
||||
break;
|
||||
case YLandingEntityType.MenuItemAlbum:
|
||||
case YLandingEntityType.MenuItemPlaylist:
|
||||
entity = jObject.ToObject<YLandingEntityAlbumMenuItem>();
|
||||
break;
|
||||
case YLandingEntityType.ClientWidget:
|
||||
entity = jObject.ToObject<YLandingEntityClientWidget>();
|
||||
break;
|
||||
default:
|
||||
entity = jObject.ToObject<YLandingEntity>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Ошибка десериализации типа \"{jObject["type"]}\".", ex);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return JArray.Load(reader)
|
||||
.Select(GetEntity)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class YLandingEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public YLandingEntityType Type { get; set; }
|
||||
}
|
||||
public string Id { get; set; }
|
||||
public YLandingEntityType Type { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using YandexMusic.API.Models.Landing.Entity.Entities;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity;
|
||||
|
||||
/// <summary>Конвертер для сущностей лендинга (десериализация в конкретные типы на основе поля type).</summary>
|
||||
public class YLandingEntityConverter : JsonConverter<List<YLandingEntity>>
|
||||
{
|
||||
public override List<YLandingEntity> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartArray)
|
||||
throw new JsonException("Ожидается массив сущностей");
|
||||
|
||||
var entities = new List<YLandingEntity>();
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndArray)
|
||||
break;
|
||||
|
||||
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<YLandingEntityType>(typeStr, true, out var entityType))
|
||||
throw new JsonException($"Неизвестный тип сущности: {typeStr}");
|
||||
|
||||
YLandingEntity? entity = null;
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
switch (entityType)
|
||||
{
|
||||
case YLandingEntityType.Album:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityAlbum>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.ChartItem:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityChart>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.PersonalPlaylist:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityPersonalPlaylist>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.PlayContext:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityPlayContext>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.Playlist:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityPlaylist>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.Podcast:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityPodcast>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.Promotion:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityPromotion>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.Category:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityCategory>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.Station:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityStation>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.MenuItemAlbum:
|
||||
case YLandingEntityType.MenuItemPlaylist:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityAlbumMenuItem>(rawText, options);
|
||||
break;
|
||||
case YLandingEntityType.ClientWidget:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntityClientWidget>(rawText, options);
|
||||
break;
|
||||
default:
|
||||
entity = JsonSerializer.Deserialize<YLandingEntity>(rawText, options);
|
||||
break;
|
||||
}
|
||||
|
||||
if (entity != null)
|
||||
entities.Add(entity);
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, List<YLandingEntity> value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,25 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Landing.Entity
|
||||
namespace YandexMusic.API.Models.Landing.Entity;
|
||||
|
||||
public enum YLandingEntityType
|
||||
{
|
||||
public enum YLandingEntityType
|
||||
{
|
||||
Album,
|
||||
[EnumMember(Value = "chart-item")]
|
||||
ChartItem,
|
||||
[EnumMember(Value = "personal-playlist")]
|
||||
PersonalPlaylist,
|
||||
[EnumMember(Value = "play-context")]
|
||||
PlayContext,
|
||||
Playlist,
|
||||
Podcast,
|
||||
Promotion,
|
||||
Category,
|
||||
Station,
|
||||
[EnumMember(Value = "menu-item-album")]
|
||||
MenuItemAlbum,
|
||||
[EnumMember(Value = "menu-item-playlist")]
|
||||
MenuItemPlaylist,
|
||||
[EnumMember(Value = "client-widget")]
|
||||
ClientWidget
|
||||
}
|
||||
Album,
|
||||
[EnumMember(Value = "chart-item")]
|
||||
ChartItem,
|
||||
[EnumMember(Value = "personal-playlist")]
|
||||
PersonalPlaylist,
|
||||
[EnumMember(Value = "play-context")]
|
||||
PlayContext,
|
||||
Playlist,
|
||||
Podcast,
|
||||
Promotion,
|
||||
Category,
|
||||
Station,
|
||||
[EnumMember(Value = "menu-item-album")]
|
||||
MenuItemAlbum,
|
||||
[EnumMember(Value = "menu-item-playlist")]
|
||||
MenuItemPlaylist,
|
||||
[EnumMember(Value = "client-widget")]
|
||||
ClientWidget,
|
||||
}
|
||||
Reference in New Issue
Block a user