using System;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MediaBrowser.Common.Json.Converters
{
///
/// Converts a string N/A to string.Empty.
///
/// The resulting type.
public class JsonOmdbNotAvailableStructConverter : JsonConverter
where T : struct
{
///
public override T? 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 (T?)converter.ConvertFromString(str);
}
return JsonSerializer.Deserialize(ref reader, options);
}
///
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(value, options);
}
}
}