diff --git a/GW2SDK.Tests/Features/Hero/Equipment/Novelties/Novelties.cs b/GW2SDK.Tests/Features/Hero/Equipment/Novelties/Novelties.cs index e0dc67d16..099327b9b 100644 --- a/GW2SDK.Tests/Features/Hero/Equipment/Novelties/Novelties.cs +++ b/GW2SDK.Tests/Features/Hero/Equipment/Novelties/Novelties.cs @@ -1,4 +1,6 @@ -using GuildWars2.Tests.Features.Markup; +using System.Text.Json; +using GuildWars2.Hero.Equipment.Novelties; +using GuildWars2.Tests.Features.Markup; using GuildWars2.Tests.TestInfrastructure; namespace GuildWars2.Tests.Features.Hero.Equipment.Novelties; @@ -28,6 +30,10 @@ public async Task Can_be_listed() Assert.NotEmpty(entry.IconHref); Assert.True(entry.Slot.IsDefined()); Assert.NotEmpty(entry.UnlockItemIds); + + var json = JsonSerializer.Serialize(entry); + var roundtrip = JsonSerializer.Deserialize(json); + Assert.Equal(entry, roundtrip); } ); } diff --git a/GW2SDK/Features/Hero/Equipment/Novelties/Novelty.cs b/GW2SDK/Features/Hero/Equipment/Novelties/Novelty.cs index 38e85cf2e..38ae6b4aa 100644 --- a/GW2SDK/Features/Hero/Equipment/Novelties/Novelty.cs +++ b/GW2SDK/Features/Hero/Equipment/Novelties/Novelty.cs @@ -1,8 +1,11 @@ -namespace GuildWars2.Hero.Equipment.Novelties; +using System.Text.Json.Serialization; + +namespace GuildWars2.Hero.Equipment.Novelties; /// Information about a novelty. [PublicAPI] [DataTransferObject] +[JsonConverter(typeof(NoveltyJsonConverter))] public sealed record Novelty { /// The novelty ID. diff --git a/GW2SDK/Features/Hero/Equipment/Novelties/NoveltyJsonConverter.cs b/GW2SDK/Features/Hero/Equipment/Novelties/NoveltyJsonConverter.cs new file mode 100644 index 000000000..a2fc13e3a --- /dev/null +++ b/GW2SDK/Features/Hero/Equipment/Novelties/NoveltyJsonConverter.cs @@ -0,0 +1,42 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using GuildWars2.Json; + +namespace GuildWars2.Hero.Equipment.Novelties; + +internal sealed class NoveltyJsonConverter : JsonConverter +{ + public override Novelty? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + using var json = JsonDocument.ParseValue(ref reader); + return Read(json.RootElement); + } + public static Novelty? Read(JsonElement json) + { + return new Novelty + { + Id = json.GetProperty("id").GetInt32(), + Name = json.GetProperty("name").GetStringRequired(), + Description = json.GetProperty("description").GetStringRequired(), + IconHref = json.GetProperty("icon").GetStringRequired(), + Slot = json.GetProperty("slot").GetStringRequired(), + UnlockItemIds = json.GetProperty("unlock_item_ids").GetList(entry => entry.GetInt32()) + }; + } + public override void Write(Utf8JsonWriter writer, Novelty value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + writer.WriteNumber("id", value.Id); + writer.WriteString("name", value.Name); + writer.WriteString("description", value.Description); + writer.WriteString("icon", value.IconHref); + writer.WriteString("slot", value.Slot.ToString()); + writer.WriteStartArray("unlock_item_ids"); + foreach (var unlockItemId in value.UnlockItemIds) + { + writer.WriteNumberValue(unlockItemId); + } + writer.WriteEndArray(); + writer.WriteEndObject(); + } +}