mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Merge pull request #4943 from crobibero/omdb-fix
This commit is contained in:
commit
6f0a6fdd5d
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Common.Json.Converters
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string <c>N/A</c> to <c>string.Empty</c>.
|
||||||
|
/// </summary>
|
||||||
|
public class JsonOmdbNotAvailableInt32Converter : JsonConverter<int?>
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
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<int?>(ref reader, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value.HasValue)
|
||||||
|
{
|
||||||
|
writer.WriteNumberValue(value.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writer.WriteNullValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@ namespace MediaBrowser.Common.Json.Converters
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
JsonSerializer.Serialize(value, options);
|
writer.WriteStringValue(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonSerializer.Deserialize<T>(ref reader, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
|
||||||
{
|
|
||||||
JsonSerializer.Serialize(value, options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -50,7 +50,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
|
|||||||
|
|
||||||
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
|
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
|
||||||
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
||||||
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStructConverter<int>());
|
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Name => "The Open Movie Database";
|
public string Name => "The Open Movie Database";
|
||||||
|
@ -41,7 +41,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
|
|||||||
|
|
||||||
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
|
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions());
|
||||||
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
_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)
|
public async Task Fetch<T>(MetadataResult<T> itemResult, string imdbId, string language, string country, CancellationToken cancellationToken)
|
||||||
@ -214,39 +214,15 @@ namespace MediaBrowser.Providers.Plugins.Omdb
|
|||||||
internal async Task<RootObject> GetRootObject(string imdbId, CancellationToken cancellationToken)
|
internal async Task<RootObject> GetRootObject(string imdbId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var path = await EnsureItemInfo(imdbId, cancellationToken).ConfigureAwait(false);
|
var path = await EnsureItemInfo(imdbId, cancellationToken).ConfigureAwait(false);
|
||||||
|
await using var stream = File.OpenRead(path);
|
||||||
string resultString;
|
return await JsonSerializer.DeserializeAsync<RootObject>(stream, _jsonOptions, cancellationToken);
|
||||||
|
|
||||||
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
||||||
{
|
|
||||||
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
|
|
||||||
{
|
|
||||||
resultString = reader.ReadToEnd();
|
|
||||||
resultString = resultString.Replace("\"N/A\"", "\"\"");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = JsonSerializer.Deserialize<RootObject>(resultString, _jsonOptions);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<SeasonRootObject> GetSeasonRootObject(string imdbId, int seasonId, CancellationToken cancellationToken)
|
internal async Task<SeasonRootObject> GetSeasonRootObject(string imdbId, int seasonId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var path = await EnsureSeasonInfo(imdbId, seasonId, cancellationToken).ConfigureAwait(false);
|
var path = await EnsureSeasonInfo(imdbId, seasonId, cancellationToken).ConfigureAwait(false);
|
||||||
|
await using var stream = File.OpenRead(path);
|
||||||
string resultString;
|
return await JsonSerializer.DeserializeAsync<SeasonRootObject>(stream, _jsonOptions, cancellationToken);
|
||||||
|
|
||||||
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
||||||
{
|
|
||||||
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
|
|
||||||
{
|
|
||||||
resultString = reader.ReadToEnd();
|
|
||||||
resultString = resultString.Replace("\"N/A\"", "\"\"");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = JsonSerializer.Deserialize<SeasonRootObject>(resultString, _jsonOptions);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool IsValidSeries(Dictionary<string, string> seriesProviderIds)
|
internal static bool IsValidSeries(Dictionary<string, string> seriesProviderIds)
|
||||||
|
@ -14,7 +14,7 @@ namespace Jellyfin.Common.Tests.Json
|
|||||||
{
|
{
|
||||||
_options = new JsonSerializerOptions();
|
_options = new JsonSerializerOptions();
|
||||||
_options.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
_options.Converters.Add(new JsonOmdbNotAvailableStringConverter());
|
||||||
_options.Converters.Add(new JsonOmdbNotAvailableStructConverter<int>());
|
_options.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
|
||||||
_options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
|
_options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,5 +64,25 @@ namespace Jellyfin.Common.Tests.Json
|
|||||||
var result = JsonSerializer.Deserialize<string>(Input, _options);
|
var result = JsonSerializer.Deserialize<string>(Input, _options);
|
||||||
Assert.Equal(Expected, result);
|
Assert.Equal(Expected, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Roundtrip_Valid_Success()
|
||||||
|
{
|
||||||
|
const string Input = "{\"Title\":\"Chapter 1\",\"Year\":\"2013\",\"Rated\":\"TV-MA\",\"Released\":\"01 Feb 2013\",\"Season\":\"N/A\",\"Episode\":\"N/A\",\"Runtime\":\"55 min\",\"Genre\":\"Drama\",\"Director\":\"David Fincher\",\"Writer\":\"Michael Dobbs (based on the novels by), Andrew Davies (based on the mini-series by), Beau Willimon (created for television by), Beau Willimon, Sam Forman (staff writer)\",\"Actors\":\"Kevin Spacey, Robin Wright, Kate Mara, Corey Stoll\",\"Plot\":\"Congressman Francis Underwood has been declined the chair for Secretary of State. He's now gathering his own team to plot his revenge. Zoe Barnes, a reporter for the Washington Herald, will do anything to get her big break.\",\"Language\":\"English\",\"Country\":\"USA\",\"Awards\":\"N/A\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMTY5MTU4NDQzNV5BMl5BanBnXkFtZTgwMzk2ODcxMzE@._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"8.7/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"8.7\",\"imdbVotes\":\"6736\",\"imdbID\":\"tt2161930\",\"seriesID\":\"N/A\",\"Type\":\"episode\",\"Response\":\"True\"}";
|
||||||
|
var trip1 = JsonSerializer.Deserialize<OmdbProvider.RootObject>(Input, _options);
|
||||||
|
Assert.NotNull(trip1);
|
||||||
|
Assert.NotNull(trip1?.Title);
|
||||||
|
Assert.Null(trip1?.Awards);
|
||||||
|
Assert.Null(trip1?.Episode);
|
||||||
|
Assert.Null(trip1?.Metascore);
|
||||||
|
|
||||||
|
var serializedTrip1 = JsonSerializer.Serialize(trip1!, _options);
|
||||||
|
var trip2 = JsonSerializer.Deserialize<OmdbProvider.RootObject>(serializedTrip1, _options);
|
||||||
|
Assert.NotNull(trip2);
|
||||||
|
Assert.NotNull(trip2?.Title);
|
||||||
|
Assert.Null(trip2?.Awards);
|
||||||
|
Assert.Null(trip2?.Episode);
|
||||||
|
Assert.Null(trip2?.Metascore);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user