Adds new JsonConverter for special ints which can have "none" as value, too.

This commit is contained in:
Serraniel 2023-03-01 21:44:13 +01:00
parent b6ff5f86a6
commit 62a4ac5c4b
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
2 changed files with 38 additions and 0 deletions

View file

@ -22,6 +22,8 @@ public class Set : EndpointObject
public string? Language { get; set; } public string? Language { get; set; }
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
[JsonConverter(typeof(NoneIntJsonConverter))] // set hashes in card objects sometimes have "none" as year.
public int Year { get; set; } public int Year { get; set; }
// TODO: According to documentation the property currently is not supported. // TODO: According to documentation the property currently is not supported.

View file

@ -0,0 +1,36 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace JpnCardsPokemonSdk.Utils.JsonConverter;
internal class NoneIntJsonConverter : JsonConverter<int>
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var valueStr = reader.GetString();
if (valueStr?.Equals("none", StringComparison.InvariantCultureIgnoreCase) ?? true)
return -1;
if (int.TryParse(valueStr, out var number))
return number;
}
else if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}
return -1;
}
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
if (value >= 0)
writer.WriteNumberValue(value);
else
writer.WriteStringValue("non");
}
}