diff options
| author | Greenback <jimcartlidge@yahoo.co.uk> | 2020-10-09 14:12:41 +0100 |
|---|---|---|
| committer | Greenback <jimcartlidge@yahoo.co.uk> | 2020-10-09 14:12:41 +0100 |
| commit | ebe650afa9e622d3f9e53e50b42ea676fdc25e6e (patch) | |
| tree | 51da5ba142a8243b0d36313ec0e54318132ff3a2 /MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs | |
| parent | 8a4f81c9a7a03c6a45a8a9331f71ba06ae0ce521 (diff) | |
| parent | 10d48b062315581adc4706530bc388d53ff232a4 (diff) | |
Merge remote-tracking branch 'upstream/master' into NetworkPR2
Diffstat (limited to 'MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs')
| -rw-r--r-- | MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs index cffc41ba3..0501f7b2a 100644 --- a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs @@ -8,37 +8,38 @@ namespace MediaBrowser.Common.Json.Converters /// Converts a nullable struct or value to/from JSON. /// Required - some clients send an empty string. /// </summary> - /// <typeparam name="T">The struct type.</typeparam> - public class JsonNullableStructConverter<T> : JsonConverter<T?> - where T : struct + /// <typeparam name="TStruct">The struct type.</typeparam> + public class JsonNullableStructConverter<TStruct> : JsonConverter<TStruct?> + where TStruct : struct { - private readonly JsonConverter<T?> _baseJsonConverter; - - /// <summary> - /// Initializes a new instance of the <see cref="JsonNullableStructConverter{T}"/> class. - /// </summary> - /// <param name="baseJsonConverter">The base json converter.</param> - public JsonNullableStructConverter(JsonConverter<T?> baseJsonConverter) - { - _baseJsonConverter = baseJsonConverter; - } - /// <inheritdoc /> - public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - // Handle empty string. + if (reader.TokenType == JsonTokenType.Null) + { + return null; + } + + // Token is empty string. if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) { return null; } - return _baseJsonConverter.Read(ref reader, typeToConvert, options); + return JsonSerializer.Deserialize<TStruct>(ref reader, options); } /// <inheritdoc /> - public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) + public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options) { - _baseJsonConverter.Write(writer, value, options); + if (value.HasValue) + { + JsonSerializer.Serialize(writer, value.Value, options); + } + else + { + writer.WriteNullValue(); + } } } } |
