mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fixing nested serializing
This commit is contained in:
parent
9a7b2cb4a1
commit
fab9a3f6a1
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user