mirror of
https://github.com/zoriya/Kyoo.git
synced 2026-05-29 18:42:29 -04:00
Adding a field property with auto loading & removing non-loaded fields from the json serialization. Reworking all json serialization
This commit is contained in:
@@ -1,13 +1,5 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Kyoo.Models;
|
||||
using Kyoo.Models.Attributes;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
@@ -19,85 +11,15 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
property.ShouldSerialize = i => member.GetCustomAttribute<JsonReadOnly>(true) == null;
|
||||
property.ShouldDeserialize = i => member.GetCustomAttribute<JsonIgnore>(true) == null;
|
||||
// TODO this get called only once and get cached.
|
||||
|
||||
// if (member?.GetCustomAttributes<LoadableRelationAttribute>() != null)
|
||||
// property.NullValueHandling = NullValueHandling.Ignore;
|
||||
// if (member?.GetCustomAttributes<SerializeIgnoreAttribute>() != null)
|
||||
// property.ShouldSerialize = _ => false;
|
||||
// if (member?.GetCustomAttributes<DeserializeIgnoreAttribute>() != null)
|
||||
// property.ShouldDeserialize = _ => false;
|
||||
return property;
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonPropertySelector : JsonPropertyIgnorer
|
||||
{
|
||||
private readonly Dictionary<Type, HashSet<string>> _ignored;
|
||||
private readonly Dictionary<Type, HashSet<string>> _forceSerialize;
|
||||
|
||||
public JsonPropertySelector()
|
||||
{
|
||||
_ignored = new Dictionary<Type, HashSet<string>>();
|
||||
_forceSerialize = new Dictionary<Type, HashSet<string>>();
|
||||
}
|
||||
|
||||
public JsonPropertySelector(Dictionary<Type, HashSet<string>> ignored,
|
||||
Dictionary<Type, HashSet<string>> forceSerialize = null)
|
||||
{
|
||||
_ignored = ignored ?? new Dictionary<Type, HashSet<string>>();
|
||||
_forceSerialize = forceSerialize ?? new Dictionary<Type, HashSet<string>>();
|
||||
}
|
||||
|
||||
private bool IsIgnored(Type type, string propertyName)
|
||||
{
|
||||
while (type != null)
|
||||
{
|
||||
if (_ignored.ContainsKey(type) && _ignored[type].Contains(propertyName))
|
||||
return true;
|
||||
type = type.BaseType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsSerializationForced(Type type, string propertyName)
|
||||
{
|
||||
while (type != null)
|
||||
{
|
||||
if (_forceSerialize.ContainsKey(type) && _forceSerialize[type].Contains(propertyName))
|
||||
return true;
|
||||
type = type.BaseType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
if (IsSerializationForced(property.DeclaringType, property.PropertyName))
|
||||
property.ShouldSerialize = i => true;
|
||||
else if (IsIgnored(property.DeclaringType, property.PropertyName))
|
||||
property.ShouldSerialize = i => false;
|
||||
return property;
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonDetailed : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (context.Result is ObjectResult result)
|
||||
{
|
||||
result.Formatters.Add(new NewtonsoftJsonOutputFormatter(
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new JsonPropertySelector(null, new Dictionary<Type, HashSet<string>>
|
||||
{
|
||||
{typeof(Show), new HashSet<string> {"genres", "studio"}},
|
||||
{typeof(Episode), new HashSet<string> {"tracks"}},
|
||||
{typeof(PeopleRole), new HashSet<string> {"show"}}
|
||||
})
|
||||
},
|
||||
context.HttpContext.RequestServices.GetRequiredService<ArrayPool<char>>(),
|
||||
new MvcOptions()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user