mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Fix images from string conversion used by the scanner
This commit is contained in:
parent
64eb70d292
commit
fee6032e78
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -38,7 +38,7 @@ public static class Utility
|
|||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
TypeInfoResolver = new JsonKindResolver(),
|
TypeInfoResolver = new JsonKindResolver(),
|
||||||
Converters = { new JsonStringEnumConverter() },
|
Converters = { new JsonStringEnumConverter(), new TypeConverterJsonAdapter() },
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -101,6 +101,7 @@ public class CoreModule : IPlugin
|
|||||||
Modifiers = { IncludeBinder.HandleLoadableFields }
|
Modifiers = { IncludeBinder.HandleLoadableFields }
|
||||||
};
|
};
|
||||||
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
x.JsonSerializerOptions.Converters.Add(new TypeConverterJsonAdapter());
|
||||||
x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||||
})
|
})
|
||||||
.AddDataAnnotations()
|
.AddDataAnnotations()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user