Fix images from string conversion used by the scanner

This commit is contained in:
Zoe Roux 2024-03-24 20:47:56 +01:00
parent 64eb70d292
commit fee6032e78
No known key found for this signature in database
3 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,67 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Microsoft.AspNetCore.Http;
using static System.Text.Json.JsonNamingPolicy;
namespace Kyoo.Utils;
public class TypeConverterJsonAdapter : JsonConverter<object>
{
public override object Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
TypeConverter converter = TypeDescriptor.GetConverter(typeToConvert);
string? text = reader.GetString();
return converter.ConvertFromString(text);
}
public override void Write(
Utf8JsonWriter writer,
object objectToWrite,
JsonSerializerOptions options
)
{
var converter = TypeDescriptor.GetConverter(objectToWrite);
var text = converter.ConvertToString(objectToWrite);
writer.WriteStringValue(text);
}
public override bool CanConvert(Type typeToConvert)
{
var hasConverter = typeToConvert
.GetCustomAttributes<TypeConverterAttribute>(inherit: true)
.Any();
return hasConverter;
}
}

View File

@ -38,7 +38,7 @@ public static class Utility
new()
{
TypeInfoResolver = new JsonKindResolver(),
Converters = { new JsonStringEnumConverter() },
Converters = { new JsonStringEnumConverter(), new TypeConverterJsonAdapter() },
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

View File

@ -101,6 +101,7 @@ public class CoreModule : IPlugin
Modifiers = { IncludeBinder.HandleLoadableFields }
};
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
x.JsonSerializerOptions.Converters.Add(new TypeConverterJsonAdapter());
x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
})
.AddDataAnnotations()