-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json converter for mail carriers
- Loading branch information
Showing
5 changed files
with
120 additions
and
3 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
5 changes: 4 additions & 1 deletion
5
GW2SDK/Features/Hero/Equipment/MailCarriers/MailCarrierFlags.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
51 changes: 51 additions & 0 deletions
51
GW2SDK/Features/Hero/Equipment/MailCarriers/MailCarrierFlagsJsonConverter.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,51 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using GuildWars2.Json; | ||
|
||
namespace GuildWars2.Hero.Equipment.MailCarriers; | ||
|
||
internal sealed class MailCarrierFlagsJsonConverter : JsonConverter<MailCarrierFlags> | ||
{ | ||
public override MailCarrierFlags? Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
using var json = JsonDocument.ParseValue(ref reader); | ||
return Read(json.RootElement); | ||
} | ||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
MailCarrierFlags value, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
Write(writer, value); | ||
} | ||
|
||
public static MailCarrierFlags Read(JsonElement json) | ||
{ | ||
return new MailCarrierFlags | ||
{ | ||
Default = json.GetProperty("default").GetBoolean(), | ||
Other = json.GetProperty("other").GetList(static value => value.GetStringRequired()) | ||
}; | ||
} | ||
|
||
public static void Write(Utf8JsonWriter writer, MailCarrierFlags value) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteBoolean("default", value.Default); | ||
writer.WritePropertyName("other"); | ||
writer.WriteStartArray(); | ||
foreach (var other in value.Other) | ||
{ | ||
writer.WriteStringValue(other); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
GW2SDK/Features/Hero/Equipment/MailCarriers/MailCarrierJsonConverter.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,54 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using GuildWars2.Json; | ||
|
||
namespace GuildWars2.Hero.Equipment.MailCarriers; | ||
|
||
internal sealed class MailCarrierJsonConverter : JsonConverter<MailCarrier> | ||
{ | ||
public override MailCarrier? Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
using var document = JsonDocument.ParseValue(ref reader); | ||
return Read(document.RootElement); | ||
} | ||
|
||
public static MailCarrier? Read(JsonElement json) | ||
{ | ||
return new MailCarrier | ||
{ | ||
Id = json.GetProperty("id").GetInt32(), | ||
UnlockItemIds = json.GetProperty("unlock_item_ids").GetList(entry => entry.GetInt32()), | ||
Order = json.GetProperty("order").GetInt32(), | ||
IconHref = json.GetProperty("icon").GetStringRequired(), | ||
Name = json.GetProperty("name").GetStringRequired(), | ||
Flags = MailCarrierFlagsJsonConverter.Read(json.GetProperty("flags")) | ||
}; | ||
} | ||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
MailCarrier value, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteNumber("id", value.Id); | ||
writer.WriteStartArray("unlock_item_ids"); | ||
foreach (var itemId in value.UnlockItemIds) | ||
{ | ||
writer.WriteNumberValue(itemId); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
writer.WriteNumber("order", value.Order); | ||
writer.WriteString("icon", value.IconHref); | ||
writer.WriteString("name", value.Name); | ||
writer.WritePropertyName("flags"); | ||
MailCarrierFlagsJsonConverter.Write(writer, value.Flags); | ||
writer.WriteEndObject(); | ||
} | ||
} |