34 lines
1.6 KiB
C#
34 lines
1.6 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace MangaReader.Core.Sources.MangaDex.Api;
|
|
|
|
public class MangaDexEntityConverter : JsonConverter<MangaDexEntity>
|
|
{
|
|
public override MangaDexEntity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
using var doc = JsonDocument.ParseValue(ref reader);
|
|
var root = doc.RootElement;
|
|
|
|
var type = root.GetProperty("type").GetString();
|
|
|
|
return type switch
|
|
{
|
|
"manga" => JsonSerializer.Deserialize<MangaEntity>(root.GetRawText(), options),
|
|
"author" => JsonSerializer.Deserialize<AuthorEntity>(root.GetRawText(), options),
|
|
"artist" => JsonSerializer.Deserialize<ArtistEntity>(root.GetRawText(), options),
|
|
"creator" => JsonSerializer.Deserialize<CreatorEntity>(root.GetRawText(), options),
|
|
"tag" => JsonSerializer.Deserialize<TagEntity>(root.GetRawText(), options),
|
|
"chapter" => JsonSerializer.Deserialize<ChapterEntity>(root.GetRawText(), options),
|
|
"scanlation_group" => JsonSerializer.Deserialize<ScanlationGroupEntity>(root.GetRawText(), options),
|
|
"cover_art" => JsonSerializer.Deserialize<CoverArtEntity>(root.GetRawText(), options),
|
|
"user" => JsonSerializer.Deserialize<UserEntity>(root.GetRawText(), options),
|
|
_ => throw new NotSupportedException($"Unknown type '{type}'")
|
|
};
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, MangaDexEntity value, JsonSerializerOptions options)
|
|
{
|
|
JsonSerializer.Serialize(writer, (object)value, options);
|
|
}
|
|
} |