| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | | using System.Text.Json; |
| | 4 | | using System.Text.Json.Serialization; |
| | 5 | |
|
| | 6 | | namespace MediaBrowser.Providers.Plugins.Omdb |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Converts a string <c>N/A</c> to <c>string.Empty</c>. |
| | 10 | | /// </summary> |
| | 11 | | public class JsonOmdbNotAvailableInt32Converter : JsonConverter<int?> |
| | 12 | | { |
| | 13 | | /// <inheritdoc /> |
| | 14 | | public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 15 | | { |
| 5 | 16 | | if (reader.TokenType == JsonTokenType.String) |
| | 17 | | { |
| 4 | 18 | | var str = reader.GetString(); |
| 4 | 19 | | if (str is null || str.Equals("N/A", StringComparison.OrdinalIgnoreCase)) |
| | 20 | | { |
| 3 | 21 | | return null; |
| | 22 | | } |
| | 23 | |
|
| 1 | 24 | | var converter = TypeDescriptor.GetConverter(typeToConvert); |
| 1 | 25 | | return (int?)converter.ConvertFromString(str); |
| | 26 | | } |
| | 27 | |
|
| 1 | 28 | | return JsonSerializer.Deserialize<int>(ref reader, options); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| | 32 | | public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) |
| | 33 | | { |
| 0 | 34 | | if (value.HasValue) |
| | 35 | | { |
| 0 | 36 | | writer.WriteNumberValue(value.Value); |
| | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 0 | 40 | | writer.WriteNullValue(); |
| | 41 | | } |
| 0 | 42 | | } |
| | 43 | | } |
| | 44 | | } |