Fixing nested serializing

This commit is contained in:
Zoe Roux 2021-03-07 18:15:14 +01:00
parent 9a7b2cb4a1
commit fab9a3f6a1
2 changed files with 15 additions and 2 deletions

View File

@ -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;
}
}
}

View File

@ -46,7 +46,7 @@ namespace Kyoo.Controllers
public override Task<Episode> Get(string slug)
{
Match match = Regex.Match(slug, @"(?<show>.*)-s(?<season>\d*)-e(?<episode>\d*)");
Match match = Regex.Match(slug, @"(?<show>.*)-s(?<season>\d*)e(?<episode>\d*)");
if (!match.Success)
return _database.Episodes.FirstOrDefaultAsync(x => x.Show.Slug == slug);