mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-16 12:04:18 -04:00
35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System.Reflection;
|
|
using Kyoo.Models.Attributes;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
namespace Kyoo.Controllers
|
|
{
|
|
public class JsonPropertyIgnorer : CamelCasePropertyNamesContractResolver
|
|
{
|
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
|
{
|
|
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
|
|
|
LoadableRelationAttribute relation = member?.GetCustomAttribute<LoadableRelationAttribute>();
|
|
if (relation != null)
|
|
{
|
|
if (relation.RelationID == null)
|
|
property.ShouldSerialize = x => member.GetValue(x) != null;
|
|
else
|
|
property.ShouldSerialize = x =>
|
|
{
|
|
if (member.GetValue(x) != null)
|
|
return true;
|
|
return x.GetType().GetProperty(relation.RelationID)?.GetValue(x) != null;
|
|
};
|
|
}
|
|
|
|
if (member?.GetCustomAttribute<SerializeIgnoreAttribute>() != null)
|
|
property.ShouldSerialize = _ => false;
|
|
if (member?.GetCustomAttribute<DeserializeIgnoreAttribute>() != null)
|
|
property.ShouldDeserialize = _ => false;
|
|
return property;
|
|
}
|
|
}
|
|
} |