Skip to content

Commit

Permalink
Add json converter for jade bot skins
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 26, 2025
1 parent c6bec95 commit b78d3a2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
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.JadeBots;
using GuildWars2.Tests.Features.Markup;
using GuildWars2.Tests.TestInfrastructure;

namespace GuildWars2.Tests.Features.Hero.Equipment.JadeBots;
Expand Down Expand Up @@ -37,6 +39,10 @@ await sut.Hero.Equipment.JadeBots.GetJadeBotSkins(
}

Assert.True(entry.UnlockItemId > 0);

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

namespace GuildWars2.Hero.Equipment.JadeBots;

/// <summary>Information about a jade bot skin.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(JadeBotSkinJsonConverter))]
public sealed record JadeBotSkin
{
/// <summary>The jade bot skin ID.</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using GuildWars2.Json;

namespace GuildWars2.Hero.Equipment.JadeBots;

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

public static JadeBotSkin? Read(JsonElement json)
{
return new JadeBotSkin
{
Id = json.GetProperty("id").GetInt32(),
Name = json.GetProperty("name").GetStringRequired(),
Description = json.GetProperty("description").GetStringRequired(),
UnlockItemId = json.GetProperty("unlock_item_id").GetInt32()
};
}

public override void Write(
Utf8JsonWriter writer,
JadeBotSkin value,
JsonSerializerOptions options
)
{
writer.WriteStartObject();
writer.WriteNumber("id", value.Id);
writer.WriteString("name", value.Name);
writer.WriteString("description", value.Description);
writer.WriteNumber("unlock_item_id", value.UnlockItemId);
writer.WriteEndObject();
}
}

0 comments on commit b78d3a2

Please sign in to comment.