Fix serialization loop

This commit is contained in:
crobibero 2021-01-04 07:52:44 -07:00
parent a3a31952f4
commit c1d1b6e9f4
5 changed files with 17 additions and 12 deletions

View File

@ -8,12 +8,10 @@ namespace MediaBrowser.Common.Json.Converters
/// <summary>
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
/// </summary>
/// <typeparam name="T">The resulting type.</typeparam>
public class JsonOmdbNotAvailableStructConverter<T> : JsonConverter<T?>
where T : struct
public class JsonOmdbNotAvailableInt32Converter : JsonConverter<int?>
{
/// <inheritdoc />
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
@ -24,16 +22,23 @@ namespace MediaBrowser.Common.Json.Converters
}
var converter = TypeDescriptor.GetConverter(typeToConvert);
return (T?)converter.ConvertFromString(str);
return (int?)converter.ConvertFromString(str);
}
return JsonSerializer.Deserialize<T>(ref reader, options);
return JsonSerializer.Deserialize<int?>(ref reader, options);
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(value, options);
if (value.HasValue)
{
writer.WriteNumberValue(value.Value);
}
else
{
writer.WriteNullValue();
}
}
}
}

View File

@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Json.Converters
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(value, options);
writer.WriteStringValue(value);
}
}
}

View File

@ -50,7 +50,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStructConverter<int>());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
}
public string Name => "The Open Movie Database";

View File

@ -41,7 +41,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStructConverter<int>());
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
}
public async Task Fetch<T>(MetadataResult<T> itemResult, string imdbId, string language, string country, CancellationToken cancellationToken)

View File

@ -14,7 +14,7 @@ namespace Jellyfin.Common.Tests.Json
{
_options = new JsonSerializerOptions();
_options.Converters.Add(new JsonOmdbNotAvailableStringConverter());
_options.Converters.Add(new JsonOmdbNotAvailableStructConverter<int>());
_options.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
_options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
}