Skip to content

Commit

Permalink
Add json converter for glider skins
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 26, 2025
1 parent dc26c19 commit c6bec95
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
8 changes: 7 additions & 1 deletion GW2SDK.Tests/Features/Hero/Equipment/Gliders/GliderSkins.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<GliderSkin>(json);
Assert.Equal(entry, roundTrip);
}
);
}
Expand Down
33 changes: 32 additions & 1 deletion GW2SDK/Features/Hero/Equipment/Gliders/GliderSkin.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace GuildWars2.Hero.Equipment.Gliders;
using System.Text.Json.Serialization;

namespace GuildWars2.Hero.Equipment.Gliders;

/// <summary>Information about a glider skin.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(GliderSkinJsonConverter))]
public sealed record GliderSkin
{
/// <summary>The glider skin ID.</summary>
Expand All @@ -26,4 +29,32 @@ public sealed record GliderSkin

/// <summary>The color IDs of the dyes applied by default.</summary>
public required IReadOnlyList<int> DefaultDyeColorIds { get; init; }

/// <inheritdoc />
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);
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(
Id,
UnlockItemIds,
Order,
IconHref,
Name,
Description,
DefaultDyeColorIds
);
}
}
53 changes: 53 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Gliders/GliderSkinJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -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<GliderSkin>
{
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();
}
}

0 comments on commit c6bec95

Please sign in to comment.