-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
GW2SDK/Features/Hero/Equipment/Novelties/NoveltyJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |