23 lines
806 B
C#
23 lines
806 B
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace JSMR.Infrastructure.Integrations.DLSite.Serialization;
|
|
|
|
public sealed class NumberConverter : JsonConverter<int>
|
|
{
|
|
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
return reader.TokenType switch
|
|
{
|
|
JsonTokenType.Number => reader.TryGetInt32(out var i) ? i : 0,
|
|
JsonTokenType.String => int.TryParse(reader.GetString(), out var j) ? j : 0,
|
|
JsonTokenType.False => 0,
|
|
JsonTokenType.True => 1,
|
|
JsonTokenType.Null => 0,
|
|
_ => 0
|
|
};
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
|
|
=> writer.WriteNumberValue(value);
|
|
} |