mirror of
https://github.com/zoriya/Kyoo.git
synced 2026-05-29 18:42:29 -04:00
Fixing nested serializing
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Kyoo.Models.Attributes;
|
||||
using Newtonsoft.Json;
|
||||
@@ -7,6 +8,8 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
public class JsonPropertyIgnorer : CamelCasePropertyNamesContractResolver
|
||||
{
|
||||
private int _depth = -1;
|
||||
|
||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
@@ -15,10 +18,12 @@ namespace Kyoo.Controllers
|
||||
if (relation != null)
|
||||
{
|
||||
if (relation.RelationID == null)
|
||||
property.ShouldSerialize = x => member.GetValue(x) != null;
|
||||
property.ShouldSerialize = x => _depth == 0 && member.GetValue(x) != null;
|
||||
else
|
||||
property.ShouldSerialize = x =>
|
||||
{
|
||||
if (_depth != 0)
|
||||
return false;
|
||||
if (member.GetValue(x) != null)
|
||||
return true;
|
||||
return x.GetType().GetProperty(relation.RelationID)?.GetValue(x) != null;
|
||||
@@ -31,5 +36,13 @@ namespace Kyoo.Controllers
|
||||
property.ShouldDeserialize = _ => false;
|
||||
return property;
|
||||
}
|
||||
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
JsonContract contract = base.CreateContract(objectType);
|
||||
contract.OnSerializingCallbacks.Add((_, _) => _depth++);
|
||||
contract.OnSerializedCallbacks.Add((_, _) => _depth--);
|
||||
return contract;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user