Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5541d0ad27 |
@@ -1,4 +1,3 @@
|
|||||||
using System.Text.Json.Serialization;
|
|
||||||
using YandexMusic.API.Models.Album;
|
using YandexMusic.API.Models.Album;
|
||||||
using YandexMusic.API.Models.Common;
|
using YandexMusic.API.Models.Common;
|
||||||
using YandexMusic.API.Models.Common.Cover;
|
using YandexMusic.API.Models.Common.Cover;
|
||||||
@@ -11,7 +10,6 @@ public class YArtistBriefInfo
|
|||||||
{
|
{
|
||||||
public YButton ActionButton { get; set; }
|
public YButton ActionButton { get; set; }
|
||||||
public List<YAlbum> Albums { get; set; }
|
public List<YAlbum> Albums { get; set; }
|
||||||
[JsonConverter(typeof(YCoverConverter))]
|
|
||||||
public List<YCover> AllCovers { get; set; }
|
public List<YCover> AllCovers { get; set; }
|
||||||
public List<YAlbum> AlsoAlbums { get; set; }
|
public List<YAlbum> AlsoAlbums { get; set; }
|
||||||
public YArtist Artist { get; set; }
|
public YArtist Artist { get; set; }
|
||||||
|
|||||||
@@ -1,33 +1,7 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace YandexMusic.API.Models.Common.Cover;
|
namespace YandexMusic.API.Models.Common.Cover;
|
||||||
|
|
||||||
public class YCoverConverter : JsonConverter<YCover>
|
|
||||||
{
|
|
||||||
public override YCover? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
||||||
{
|
|
||||||
if (reader.TokenType != JsonTokenType.StartObject) return null;
|
|
||||||
using var doc = JsonDocument.ParseValue(ref reader);
|
|
||||||
var root = doc.RootElement;
|
|
||||||
var type = root.TryGetProperty("type", out var t) ? t.GetString() : null;
|
|
||||||
if (root.TryGetProperty("error", out _)) type = "error";
|
|
||||||
|
|
||||||
return type switch
|
|
||||||
{
|
|
||||||
"color" => JsonSerializer.Deserialize<YCoverColor>(root.GetRawText(), options),
|
|
||||||
"error" => JsonSerializer.Deserialize<YCoverError>(root.GetRawText(), options),
|
|
||||||
"from-artist-photos" or "from-album-cover" => JsonSerializer.Deserialize<YCoverImage>(root.GetRawText(), options),
|
|
||||||
"pic" => JsonSerializer.Deserialize<YCoverPic>(root.GetRawText(), options),
|
|
||||||
"mosaic" => JsonSerializer.Deserialize<YCoverMosaic>(root.GetRawText(), options),
|
|
||||||
_ => new YCover() { Type = YCoverType.Error }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, YCover value, JsonSerializerOptions options)
|
|
||||||
=> JsonSerializer.Serialize(writer, value, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonConverter(typeof(YCoverConverter))]
|
[JsonConverter(typeof(YCoverConverter))]
|
||||||
public class YCover
|
public class YCover
|
||||||
{
|
{
|
||||||
|
|||||||
29
YandexMusic.API/Models/Common/Cover/YCoverConverter.cs
Normal file
29
YandexMusic.API/Models/Common/Cover/YCoverConverter.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace YandexMusic.API.Models.Common.Cover;
|
||||||
|
|
||||||
|
public class YCoverConverter : JsonConverter<YCover>
|
||||||
|
{
|
||||||
|
public override YCover? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (reader.TokenType != JsonTokenType.StartObject) return null;
|
||||||
|
using var doc = JsonDocument.ParseValue(ref reader);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
var type = root.TryGetProperty("type", out var t) ? t.GetString() : null;
|
||||||
|
if (root.TryGetProperty("error", out _)) type = "error";
|
||||||
|
|
||||||
|
return type switch
|
||||||
|
{
|
||||||
|
"color" => JsonSerializer.Deserialize<YCoverColor>(root.GetRawText(), options),
|
||||||
|
"error" => JsonSerializer.Deserialize<YCoverError>(root.GetRawText(), options),
|
||||||
|
"from-artist-photos" or "from-album-cover" => JsonSerializer.Deserialize<YCoverImage>(root.GetRawText(), options),
|
||||||
|
"pic" => JsonSerializer.Deserialize<YCoverPic>(root.GetRawText(), options),
|
||||||
|
"mosaic" => JsonSerializer.Deserialize<YCoverMosaic>(root.GetRawText(), options),
|
||||||
|
_ => new YCover() { Type = YCoverType.Error }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, YCover value, JsonSerializerOptions options)
|
||||||
|
=> JsonSerializer.Serialize(writer, value, options);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user