diff options
| author | Bond-009 <bond.009@outlook.com> | 2020-08-21 23:42:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-21 23:42:47 +0200 |
| commit | 4d64041a924f1d32e63e3c111e0d073044c443b3 (patch) | |
| tree | fff31c43462a9626b4cf2f25ec4c3297290f8320 | |
| parent | 1073d41c4de6bf339e463aa39b20ed426b7430fb (diff) | |
| parent | 33f6e410ac5e90029ffcc61806a35302874c36c8 (diff) | |
Merge pull request #3951 from crobibero/json-nullable-number
Add nullable int32, int64 json converters
4 files changed, 128 insertions, 1 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs index 70c375b8c..7ed9d6766 100644 --- a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs @@ -7,7 +7,7 @@ using System.Text.Json.Serialization; namespace MediaBrowser.Common.Json.Converters { /// <summary> - /// Converts a GUID object or value to/from JSON. + /// Converts a int32 object or value to/from JSON. /// </summary> public class JsonInt32Converter : JsonConverter<int> { diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs new file mode 100644 index 000000000..c1660fe76 --- /dev/null +++ b/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs @@ -0,0 +1,55 @@ +using System; +using System.Buffers; +using System.Buffers.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace MediaBrowser.Common.Json.Converters +{ + /// <summary> + /// Converts a nullable int32 object or value to/from JSON. + /// </summary> + public class JsonNullableInt32Converter : JsonConverter<int?> + { + /// <inheritdoc /> + public override int? 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 int number, out int bytesConsumed) && span.Length == bytesConsumed) + { + return number; + } + + var stringValue = reader.GetString().AsSpan(); + + // value is null or empty, just return null. + if (stringValue.IsEmpty) + { + return null; + } + + if (int.TryParse(stringValue, out number)) + { + return number; + } + } + + return reader.GetInt32(); + } + + /// <inheritdoc /> + public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) + { + if (value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteNumberValue(value.Value); + } + } + } +} 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); + } + } + } +} diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index 36ab6d900..0a661934e 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -29,9 +29,11 @@ namespace MediaBrowser.Common.Json options.Converters.Add(new JsonGuidConverter()); options.Converters.Add(new JsonInt32Converter()); + options.Converters.Add(new JsonNullableInt32Converter()); options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory()); options.Converters.Add(new JsonInt64Converter()); + options.Converters.Add(new JsonNullableInt64Converter()); options.Converters.Add(new JsonDoubleConverter()); return options; |
