Skip to content

Commit

Permalink
Add json converter for skiffs
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 26, 2025
1 parent efc00df commit 37c3081
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
16 changes: 15 additions & 1 deletion GW2SDK.Tests/Features/Hero/Equipment/Skiffs/SkiffSkins.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GuildWars2.Tests.TestInfrastructure;
using System.Text.Json;
using GuildWars2.Hero.Equipment.Skiffs;
using GuildWars2.Tests.TestInfrastructure;

namespace GuildWars2.Tests.Features.Hero.Equipment.Skiffs;

Expand All @@ -24,6 +26,18 @@ public async Task Can_be_listed()
Assert.NotEmpty(entry.Name);
Assert.NotEmpty(entry.IconHref);
Assert.NotNull(entry.DyeSlots);
Assert.All(
entry.DyeSlots,
dyeSlot =>
{
Assert.True(dyeSlot.Material.IsDefined());
Assert.True(dyeSlot.ColorId > 0);
}
);

var json = JsonSerializer.Serialize(entry);
var roundtrip = JsonSerializer.Deserialize<SkiffSkin>(json);
Assert.Equal(entry, roundtrip);
}
);
}
Expand Down
5 changes: 4 additions & 1 deletion GW2SDK/Features/Hero/Equipment/Skiffs/SkiffSkin.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace GuildWars2.Hero.Equipment.Skiffs;
using System.Text.Json.Serialization;

namespace GuildWars2.Hero.Equipment.Skiffs;

/// <summary>Information about a skiff skin.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(SkiffSkinJsonConverter))]
public sealed record SkiffSkin
{
/// <summary>The skiff skin ID.</summary>
Expand Down
40 changes: 40 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Skiffs/SkiffSkinJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using GuildWars2.Json;

namespace GuildWars2.Hero.Equipment.Skiffs;

internal sealed class SkiffSkinJsonConverter : JsonConverter<SkiffSkin>
{
public override SkiffSkin? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var json = JsonDocument.ParseValue(ref reader);
return Read(json.RootElement);
}

public static SkiffSkin Read(JsonElement json)
{
return new SkiffSkin
{
Id = json.GetProperty("id").GetInt32(),
Name = json.GetProperty("name").GetStringRequired(),
IconHref = json.GetProperty("icon").GetStringRequired(),
DyeSlots = json.GetProperty("dye_slots").GetList(DyeSlotJsonConverter.Read)!
};
}

public override void Write(Utf8JsonWriter writer, SkiffSkin value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("id", value.Id);
writer.WriteString("name", value.Name);
writer.WriteString("icon", value.IconHref);
writer.WriteStartArray("dye_slots");
foreach (var dyeSlot in value.DyeSlots)
{
DyeSlotJsonConverter.Write(writer, dyeSlot);
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}

0 comments on commit 37c3081

Please sign in to comment.