34 lines
895 B
C#
34 lines
895 B
C#
using System.Buffers;
|
|
using System.Buffers.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace Atomx.Utils.Json.Converts
|
|
{
|
|
public class LongJsonConverter : System.Text.Json.Serialization.JsonConverter<long>
|
|
{
|
|
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
|
if (Utf8Parser.TryParse(span, out long number, out int bytesConsumed) && span.Length == bytesConsumed)
|
|
{
|
|
return number;
|
|
}
|
|
|
|
if (long.TryParse(reader.GetString(), out number))
|
|
{
|
|
return number;
|
|
}
|
|
}
|
|
|
|
return reader.GetInt64();
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(value.ToString());
|
|
}
|
|
}
|
|
}
|