diff options
| author | crobibero <cody@robibe.ro> | 2020-09-01 09:19:22 -0600 |
|---|---|---|
| committer | crobibero <cody@robibe.ro> | 2020-09-01 09:19:22 -0600 |
| commit | eb400f72928c9a87362c7d763be4627a7f9cd5cb (patch) | |
| tree | cfb35431ccdda7b01e1ea953b9d76ad5de483cb6 | |
| parent | 1f2d73af8e986b945c53a4a8bc1be1124216589e (diff) | |
Fallback to base jsonconverter
3 files changed, 41 insertions, 46 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs index cd0017c78..a4ecc542e 100644 --- a/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonNullableInt32Converter.cs @@ -10,31 +10,32 @@ namespace MediaBrowser.Common.Json.Converters /// </summary> public class JsonNullableInt32Converter : JsonConverter<int?> { + private readonly JsonConverter<int?> _baseJsonConverter; + + /// <summary> + /// Initializes a new instance of the <see cref="JsonNullableInt32Converter"/> class. + /// </summary> + /// <param name="baseJsonConverter">The base json converter.</param> + public JsonNullableInt32Converter(JsonConverter<int?> baseJsonConverter) + { + _baseJsonConverter = baseJsonConverter; + } + /// <inheritdoc /> public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - switch (reader.TokenType) + if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) { - case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty: - case JsonTokenType.Null: - return null; - default: - // fallback to default handling - return reader.GetInt32(); + return null; } + + return _baseJsonConverter.Read(ref reader, typeToConvert, options); } /// <inheritdoc /> public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) { - if (value is null) - { - writer.WriteNullValue(); - } - else - { - writer.WriteNumberValue(value.Value); - } + _baseJsonConverter.Write(writer, value, options); } } } diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs index 8c6879ac7..1745a0b09 100644 --- a/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs @@ -1,52 +1,42 @@ using System; +using System.ComponentModel; 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. + /// Converts a nullable int64 object or value to/from JSON. /// Required - some clients send an empty string. /// </summary> public class JsonNullableInt64Converter : JsonConverter<long?> { + private readonly JsonConverter<long?> _baseJsonConverter; + /// <summary> - /// Read JSON string as int64. + /// Initializes a new instance of the <see cref="JsonNullableInt64Converter"/> class. /// </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) + /// <param name="baseJsonConverter">The base json converter.</param> + public JsonNullableInt64Converter(JsonConverter<long?> baseJsonConverter) + { + _baseJsonConverter = baseJsonConverter; + } + + /// <inheritdoc /> + public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - switch (reader.TokenType) + if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) { - case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty: - case JsonTokenType.Null: - return null; - default: - // fallback to default handling - return reader.GetInt64(); + return null; } + + return _baseJsonConverter.Read(ref reader, typeToConvert, options); } - /// <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> + /// <inheritdoc /> public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options) { - if (value is null) - { - writer.WriteNullValue(); - } - else - { - writer.WriteNumberValue(value.Value); - } + _baseJsonConverter.Write(writer, value, options); } } } diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index 3f74c896f..bbdd1029a 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -29,10 +29,14 @@ namespace MediaBrowser.Common.Json NumberHandling = JsonNumberHandling.AllowReadingFromString }; + // Get built-in converters for fallback converting. + var baseNullableInt32Converter = (JsonConverter<int?>)options.GetConverter(typeof(int?)); + var baseNullableInt64Converter = (JsonConverter<long?>)options.GetConverter(typeof(long?)); + options.Converters.Add(new JsonGuidConverter()); options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new JsonNullableInt32Converter()); - options.Converters.Add(new JsonNullableInt64Converter()); + options.Converters.Add(new JsonNullableInt32Converter(baseNullableInt32Converter)); + options.Converters.Add(new JsonNullableInt64Converter(baseNullableInt64Converter)); return options; } |
