#nullable enable using System; using System.ComponentModel; using System.Text.Json; using System.Text.Json.Serialization; namespace MediaBrowser.Providers.Plugins.Omdb { /// /// Converts a string N/A to string.Empty. /// public class JsonOmdbNotAvailableInt32Converter : JsonConverter { /// public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) { var str = reader.GetString(); if (str != null && str.Equals("N/A", StringComparison.OrdinalIgnoreCase)) { return null; } var converter = TypeDescriptor.GetConverter(typeToConvert); return (int?)converter.ConvertFromString(str); } return JsonSerializer.Deserialize(ref reader, options); } /// public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options) { if (value.HasValue) { writer.WriteNumberValue(value.Value); } else { writer.WriteNullValue(); } } } }