diff options
| author | Matt Montgomery <33811686+ConfusedPolarBear@users.noreply.github.com> | 2020-08-26 10:29:37 -0500 |
|---|---|---|
| committer | Matt Montgomery <33811686+ConfusedPolarBear@users.noreply.github.com> | 2020-08-26 10:29:37 -0500 |
| commit | 2974a0248a5941f8b784a7bc99c17b1080b7d06f (patch) | |
| tree | 1eec54868d88648684ca96e1ed6405e4372d5a95 /MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs | |
| parent | 1ff4f8e6c64b453eb9096b8da09f4041dbd463fc (diff) | |
| parent | 4e3f26b647a9fe996b5a96ea10fa1f2468ea41fb (diff) | |
Merge remote-tracking branch 'upstream/master' into quickconnect
Diffstat (limited to 'MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs')
| -rw-r--r-- | MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs new file mode 100644 index 000000000..53e5f6e9d --- /dev/null +++ b/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs @@ -0,0 +1,70 @@ +using System; +using System.Buffers; +using System.Buffers.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace MediaBrowser.Common.Json.Converters +{ + /// <summary> + /// Parse JSON string as nullable long. + /// Javascript does not support 64-bit integers. + /// </summary> + public class JsonNullableInt64Converter : JsonConverter<long?> + { + /// <summary> + /// Read JSON string as int64. + /// </summary> + /// <param name="reader"><see cref="Utf8JsonReader"/>.</param> + /// <param name="type">Type.</param> + /// <param name="options">Options.</param> + /// <returns>Parsed value.</returns> + 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; + } + + var stringValue = reader.GetString().AsSpan(); + + // value is null or empty, just return null. + if (stringValue.IsEmpty) + { + return null; + } + + // try to parse from a string if the above failed, this covers cases with other escaped/UTF characters + if (long.TryParse(stringValue, out number)) + { + return number; + } + } + + // fallback to default handling + return reader.GetInt64(); + } + + /// <summary> + /// Write long to JSON long. + /// </summary> + /// <param name="writer"><see cref="Utf8JsonWriter"/>.</param> + /// <param name="value">Value to write.</param> + /// <param name="options">Options.</param> + public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options) + { + if (value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteNumberValue(value.Value); + } + } + } +} |
