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 { /// /// Double to String JSON converter. /// Web client send quoted doubles. /// public class JsonDoubleConverter : JsonConverter { /// /// Read JSON string as double. /// /// . /// Type. /// Options. /// Parsed value. public override double Read(ref Utf8JsonReader reader, Type typeToConvert, 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 double 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 (double.TryParse(reader.GetString(), out number)) { return number; } } // fallback to default handling return reader.GetDouble(); } /// /// Write double to JSON string. /// /// . /// Value to write. /// Options. public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) { writer.WriteNumberValue(value); } } }