using System; using System.Collections; using System.Reflection; using Kyoo.Models; using Kyoo.Models.Attributes; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; 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); LoadableRelationAttribute relation = member?.GetCustomAttribute(); if (relation != null) { if (relation.RelationID == 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; }; } if (member?.GetCustomAttribute() != null) property.ShouldSerialize = _ => false; if (member?.GetCustomAttribute() != null) property.ShouldDeserialize = _ => false; return property; } protected override JsonContract CreateContract(Type objectType) { JsonContract contract = base.CreateContract(objectType); if (Utility.GetGenericDefinition(objectType, typeof(Page<>)) == null && !objectType.IsAssignableTo(typeof(IEnumerable))) { contract.OnSerializingCallbacks.Add((_, _) => _depth++); contract.OnSerializedCallbacks.Add((_, _) => _depth--); } return contract; } } public class PeopleRoleConverter : JsonConverter { public override void WriteJson(JsonWriter writer, PeopleRole value, JsonSerializer serializer) { // TODO this seems to not use the property.ShouldSerialize and cause an recursive inclusion error. JToken t = JToken.FromObject(value, serializer); JObject obj = t as JObject; writer.WriteValue(obj); } public override PeopleRole ReadJson(JsonReader reader, Type objectType, PeopleRole existingValue, bool hasExistingValue, JsonSerializer serializer) { throw new NotImplementedException(); } } }