using System; using System.Buffers; using System.Buffers.Text; using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; namespace MediaBrowser.Common.Json.Converters { /// /// Parse JSON string as long. /// Javascript does not support 64-bit integers. /// public class JsonInt64Converter : JsonConverter { /// /// Read JSON string as int64. /// /// . /// Type. /// Options. /// Parsed value. public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) { // try to parse number directly from bytes var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed) { return number; } // try to parse from a string if the above failed, this covers cases with other escaped/UTF characters if (long.TryParse(reader.GetString(), out number)) { return number; } } // fallback to default handling return reader.GetInt64(); } /// /// Write long to JSON long. /// /// . /// Value to write. /// Options. public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options) { writer.WriteNumberValue(value); } } }