From c6bec95227a1a8e4192830c65f16ec6e062789ba Mon Sep 17 00:00:00 2001 From: Steven Liekens Date: Sun, 26 Jan 2025 01:11:35 +0100 Subject: [PATCH] Add json converter for glider skins --- .../Hero/Equipment/Gliders/GliderSkins.cs | 8 ++- .../Hero/Equipment/Gliders/GliderSkin.cs | 33 +++++++++++- .../Gliders/GliderSkinJsonConverter.cs | 53 +++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 GW2SDK/Features/Hero/Equipment/Gliders/GliderSkinJsonConverter.cs diff --git a/GW2SDK.Tests/Features/Hero/Equipment/Gliders/GliderSkins.cs b/GW2SDK.Tests/Features/Hero/Equipment/Gliders/GliderSkins.cs index b22ab2f48..6bf2b89b4 100644 --- a/GW2SDK.Tests/Features/Hero/Equipment/Gliders/GliderSkins.cs +++ b/GW2SDK.Tests/Features/Hero/Equipment/Gliders/GliderSkins.cs @@ -1,4 +1,6 @@ -using GuildWars2.Tests.Features.Markup; +using System.Text.Json; +using GuildWars2.Hero.Equipment.Gliders; +using GuildWars2.Tests.Features.Markup; using GuildWars2.Tests.TestInfrastructure; namespace GuildWars2.Tests.Features.Hero.Equipment.Gliders; @@ -29,6 +31,10 @@ public async Task Can_be_listed() Assert.NotNull(entry.Description); MarkupSyntaxValidator.Validate(entry.Description); Assert.NotNull(entry.DefaultDyeColorIds); + + var json = JsonSerializer.Serialize(entry); + var roundTrip = JsonSerializer.Deserialize(json); + Assert.Equal(entry, roundTrip); } ); } diff --git a/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkin.cs b/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkin.cs index dc63321a0..1283c4488 100644 --- a/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkin.cs +++ b/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkin.cs @@ -1,8 +1,11 @@ -namespace GuildWars2.Hero.Equipment.Gliders; +using System.Text.Json.Serialization; + +namespace GuildWars2.Hero.Equipment.Gliders; /// Information about a glider skin. [PublicAPI] [DataTransferObject] +[JsonConverter(typeof(GliderSkinJsonConverter))] public sealed record GliderSkin { /// The glider skin ID. @@ -26,4 +29,32 @@ public sealed record GliderSkin /// The color IDs of the dyes applied by default. public required IReadOnlyList DefaultDyeColorIds { get; init; } + + /// + public bool Equals(GliderSkin? other) + { + if (other is null) return false; + if (ReferenceEquals(this, other)) return true; + return Id == other.Id + && UnlockItemIds.SequenceEqual(other.UnlockItemIds) + && Order == other.Order + && IconHref == other.IconHref + && Name == other.Name + && Description == other.Description + && DefaultDyeColorIds.SequenceEqual(other.DefaultDyeColorIds); + } + + /// + public override int GetHashCode() + { + return HashCode.Combine( + Id, + UnlockItemIds, + Order, + IconHref, + Name, + Description, + DefaultDyeColorIds + ); + } } diff --git a/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkinJsonConverter.cs b/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkinJsonConverter.cs new file mode 100644 index 000000000..98c1bd54e --- /dev/null +++ b/GW2SDK/Features/Hero/Equipment/Gliders/GliderSkinJsonConverter.cs @@ -0,0 +1,53 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using GuildWars2.Json; + +namespace GuildWars2.Hero.Equipment.Gliders; + +internal sealed class GliderSkinJsonConverter : JsonConverter +{ + public override GliderSkin? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + using var document = JsonDocument.ParseValue(ref reader); + return Read(document.RootElement); + } + + public static GliderSkin? Read(JsonElement json) + { + return new GliderSkin + { + Id = json.GetProperty("id").GetInt32(), + UnlockItemIds = json.GetProperty("unlock_item_ids").GetList(item => item.GetInt32()), + Order = json.GetProperty("order").GetInt32(), + IconHref = json.GetProperty("icon").GetStringRequired(), + Name = json.GetProperty("name").GetStringRequired(), + Description = json.GetProperty("description").GetStringRequired(), + DefaultDyeColorIds = json.GetProperty("default_dye_color_ids").GetList(item => item.GetInt32()) + }; + } + + public override void Write(Utf8JsonWriter writer, GliderSkin value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + writer.WriteNumber("id", value.Id); + writer.WriteStartArray("unlock_item_ids"); + foreach (var element in value.UnlockItemIds) + { + writer.WriteNumberValue(element); + } + + writer.WriteEndArray(); + writer.WriteNumber("order", value.Order); + writer.WriteString("icon", value.IconHref); + writer.WriteString("name", value.Name); + writer.WriteString("description", value.Description); + writer.WriteStartArray("default_dye_color_ids"); + foreach (var element in value.DefaultDyeColorIds) + { + writer.WriteNumberValue(element); + } + + writer.WriteEndArray(); + writer.WriteEndObject(); + } +}