Skip to content

Commit

Permalink
Add json converter for novelties
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 26, 2025
1 parent a44124b commit c3b0dd2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
8 changes: 7 additions & 1 deletion GW2SDK.Tests/Features/Hero/Equipment/Novelties/Novelties.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.Novelties;
using GuildWars2.Tests.Features.Markup;
using GuildWars2.Tests.TestInfrastructure;

namespace GuildWars2.Tests.Features.Hero.Equipment.Novelties;
Expand Down Expand Up @@ -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<Novelty>(json);
Assert.Equal(entry, roundtrip);
}
);
}
Expand Down
5 changes: 4 additions & 1 deletion GW2SDK/Features/Hero/Equipment/Novelties/Novelty.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace GuildWars2.Hero.Equipment.Novelties;
using System.Text.Json.Serialization;

namespace GuildWars2.Hero.Equipment.Novelties;

/// <summary>Information about a novelty.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(NoveltyJsonConverter))]
public sealed record Novelty
{
/// <summary>The novelty ID.</summary>
Expand Down
42 changes: 42 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Novelties/NoveltyJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -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<Novelty>
{
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();
}
}

0 comments on commit c3b0dd2

Please sign in to comment.