aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Serialization/SerializableEntityContractResolver.cs
blob: 1badbc23dc54e1439f00cf659b1facd53764bd72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Tango.BL.Serialization
{
    public class SerializableEntityContractResolver : DefaultContractResolver
    {
        private List<String> _ignoreProperties = new List<string>();
        private EntitySerializationFlags _flags;

        public SerializableEntityContractResolver(EntitySerializationFlags flags)
        {
            _flags = flags;
        }

        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            _ignoreProperties = new List<string>();

            if (typeof(ISerializableEntity).IsAssignableFrom(objectType))
            {
                _ignoreProperties = (Activator.CreateInstance(objectType) as ISerializableEntity).GetIgnoreProperties(_flags);
            }
            return base.CreateObjectContract(objectType);
        }

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var props = _ignoreProperties.ToList();
            JsonProperty property = base.CreateProperty(member, memberSerialization);
            property.Ignored = false;
            property.ShouldSerialize = (x) => !props.Contains(property.PropertyName);
            return property;
        }
    }
}