Skip to content

Commit

Permalink
Add json converters for finishers
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 25, 2025
1 parent 997e6e9 commit dc26c19
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
8 changes: 7 additions & 1 deletion GW2SDK.Tests/Features/Hero/Equipment/Finishers/Finishers.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.Finishers;
using GuildWars2.Tests.Features.Markup;
using GuildWars2.Tests.TestInfrastructure;

namespace GuildWars2.Tests.Features.Hero.Equipment.Finishers;
Expand Down Expand Up @@ -29,6 +31,10 @@ public async Task Can_be_listed()
Assert.True(entry.Order >= 0);
Assert.NotEmpty(entry.IconHref);
Assert.NotEmpty(entry.Name);

var json = JsonSerializer.Serialize(entry);
var roundTrip = JsonSerializer.Deserialize<Finisher>(json);
Assert.Equal(entry, roundTrip);
}
);
}
Expand Down
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.Finishers;
using GuildWars2.Tests.TestInfrastructure;

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

Expand Down Expand Up @@ -29,6 +31,10 @@ public async Task Can_be_listed()
{
Assert.True(entry.Quantity >= 0);
}

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

namespace GuildWars2.Hero.Equipment.Finishers;

/// <summary>Information about a finisher.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(FinisherJsonConverter))]
public sealed record Finisher
{
/// <summary>The finisher ID.</summary>
Expand All @@ -22,4 +25,23 @@ public sealed record Finisher

/// <summary>The name of the finisher.</summary>
public required string Name { get; init; }

/// <inheritdoc />
public bool Equals(Finisher? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id
&& LockedText == other.LockedText
&& UnlockItemIds.SequenceEqual(other.UnlockItemIds)
&& Order == other.Order
&& IconHref == other.IconHref
&& Name == other.Name;
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(Id, LockedText, UnlockItemIds, Order, IconHref, Name);
}
}
50 changes: 50 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Finishers/FinisherJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using GuildWars2.Json;

namespace GuildWars2.Hero.Equipment.Finishers;

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

public static Finisher? Read(JsonElement json)
{
return new Finisher
{
Id = json.GetProperty("id").GetInt32(),
LockedText = json.GetProperty("locked_text").GetStringRequired(),
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()
};
}

public override void Write(Utf8JsonWriter writer, Finisher value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("id", value.Id);
writer.WriteString("locked_text", value.LockedText);
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.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using GuildWars2.Json;

namespace GuildWars2.Hero.Equipment.Finishers;

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

public static UnlockedFinisher? Read(JsonElement json)
{
return new UnlockedFinisher
{
Id = json.GetProperty("id").GetInt32(),
Permanent = json.GetProperty("permanent").GetBoolean(),
Quantity = json.GetProperty("quantity").GetNullableInt32()
};
}

public override void Write(
Utf8JsonWriter writer,
UnlockedFinisher value,
JsonSerializerOptions options
)
{
writer.WriteStartObject();
writer.WriteNumber("id", value.Id);
writer.WriteBoolean("permanent", value.Permanent);
writer.WritePropertyName("quantity");
if (value.Quantity is not null)
{
writer.WriteNumberValue(value.Quantity.Value);
}
else
{
writer.WriteNullValue();
}

writer.WriteEndObject();
}
}

0 comments on commit dc26c19

Please sign in to comment.