Adds new JsonConverter for special ints which can have "none" as value, too.
This commit is contained in:
parent
b6ff5f86a6
commit
62a4ac5c4b
|
@ -22,6 +22,8 @@ public class Set : EndpointObject
|
|||
|
||||
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; }
|
||||
|
||||
// TODO: According to documentation the property currently is not supported.
|
||||
|
|
36
src/Utils/JsonConverter/NoneIntJsonConverter.cs
Normal file
36
src/Utils/JsonConverter/NoneIntJsonConverter.cs
Normal 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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue