繁体   English   中英

使用Json.Net仅序列化选定的属性

[英]Serializing only selected properties with Json.Net

我想使用Json.NET仅序列化对象的某些属性。
我正在使用像Json.net帖子中描述的解决方案仅序列化某些属性
我的问题是我想每次都选择不同的属性,并且出于性能原因正在缓存对CreateContract (后者又调用CreateProperties )的调用(源代码: https//github.com/JamesNK/Newtonsoft.Json/blob /master/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs )。

有没有办法只序列化我想要的属性,每次都指定不同的属性,可能没有重写整个DefaultContractResolver类?

这是一个显示此问题的程序:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Person {
        public int Id;
        public string FirstName;
        public string LastName;
    }

    public class SelectedPropertiesContractResolver<T> : CamelCasePropertyNamesContractResolver {

        HashSet<string> _selectedProperties;

        public SelectedPropertiesContractResolver(IEnumerable<string> selectedProperties) {
            _selectedProperties = selectedProperties.ToHashSet();
        }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) {
            if (type == typeof(T)) {
                return base.CreateProperties(type, memberSerialization)
                    .Where(p => _selectedProperties.Contains(p.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
            }
            return base.CreateProperties(type, memberSerialization);
        }

    }


    class Program {

        static void Main(string[] args) {

            var person = new Person { Id = 1, FirstName = "John", LastName = "Doe" };

            var serializer1 = new JsonSerializer {
                ContractResolver = new SelectedPropertiesContractResolver<Person>(new[] { "Id", "FirstName" })
            };

            // This will contain only Id and FirstName, as expected
            string json1 = JObject.FromObject(person, serializer1).ToString();

            var serializer2 = new JsonSerializer {
                ContractResolver = new SelectedPropertiesContractResolver<Person>(new[] { "LastName" })
            };

            // Since calls to CreateProperties are cached, this will contain Id and FirstName as well, instead of LastName.
            string json2 = JObject.FromObject(person, serializer2).ToString();

        }
    }

您可以覆盖ResolveContract方法并始终创建新契约(甚至更好 - 根据类型T_selectedProperties内容提供您自己喜欢的缓存方式)

public class SelectedPropertiesContractResolver<T> : CamelCasePropertyNamesContractResolver {

    ...

    public override JsonContract ResolveContract(Type type)
    {
        return CreateContract(type);
    }
}

您可以使用该代码来解决您的任务:

        static void Main(string[] args)
        {
            var myObject = new {Id = 123, Name = "Test", IsTest = true};
            var propertyForSerialization = new List<string> { "Id", "Name" };

            var result = GetSerializedObject(myObject, propertyForSerialization);
        }

        private static string GetSerializedObject(object objForSerialize, List<string> propertyForSerialization)
        {
            var customObject = new ExpandoObject() as IDictionary<string, Object>;
            Type myType = objForSerialize.GetType();

            IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

            foreach (PropertyInfo prop in props)
            {
                foreach (var propForSer in propertyForSerialization)
                {
                    if (prop.Name == propForSer)
                    {
                        customObject.Add(prop.Name, prop.GetValue(objForSerialize, null));
                    }
                }
            }

           return JsonConvert.SerializeObject(customObject);
        }

基于评论和所选答案的几种可能的解决方案。

使用条件序列化:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    public interface ISerializeSelectedPropertiesOnly {
        bool ShouldSerialize(string propertyName);
    }

    public class Person : ISerializeSelectedPropertiesOnly {
        public int Id;
        public string FirstName;
        public string LastName;
        public HashSet<string> _propertiesToSerialize;
        public bool ShouldSerialize(string propertyName) {
            return _propertiesToSerialize?.Contains(propertyName, StringComparer.OrdinalIgnoreCase) ?? true;
        }
    }

    public class SelectedPropertiesContractResolver : CamelCasePropertyNamesContractResolver {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) {
            JsonProperty property = base.CreateProperty(member, memberSerialization);
            if (typeof(ISerializeSelectedPropertiesOnly).IsAssignableFrom(property.DeclaringType)) {
                property.ShouldSerialize = instance => ((ISerializeSelectedPropertiesOnly)instance).ShouldSerialize(property.PropertyName);
            }
            return property;
        }
    }

    class Program {
        static void Main(string[] args) {
            var person = new Person { Id = 1, FirstName = "John", LastName = "Doe" };
            person._propertiesToSerialize = new HashSet<string> { "Id", "FirstName" };
            var serializer = new JsonSerializer {
                ContractResolver = new SelectedPropertiesContractResolver()
            };
            string json1 = JObject.FromObject(person, serializer).ToString();
            person._propertiesToSerialize = new HashSet<string> { "LastName" };
            string json2 = JObject.FromObject(person, serializer).ToString();
        }
    }

覆盖ResolveContract

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Person {
        public int Id;
        public string FirstName;
        public string LastName;
    }

    public class SelectedPropertiesContractResolver<T> : CamelCasePropertyNamesContractResolver {
        HashSet<string> _selectedProperties;
        public SelectedPropertiesContractResolver(IEnumerable<string> selectedProperties) {
            _selectedProperties = selectedProperties.ToHashSet();
        }
        public override JsonContract ResolveContract(Type type) {
            return CreateContract(type);
        }
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) {
            if (type == typeof(T)) {
                return base.CreateProperties(type, memberSerialization)
                    .Where(p => _selectedProperties.Contains(p.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
            }
            return base.CreateProperties(type, memberSerialization);
        }
    }


    class Program {
        static void Main(string[] args) {
            var person = new Person { Id = 1, FirstName = "John", LastName = "Doe" };
            var serializer = new JsonSerializer {
                ContractResolver = new SelectedPropertiesContractResolver<Person>(new HashSet<string> { "Id", "FirstName" })
            };
            string json1 = JObject.FromObject(person, serializer).ToString();
            serializer = new JsonSerializer {
                ContractResolver = new SelectedPropertiesContractResolver<Person>(new HashSet<string> { "LastName" })
            };
            string json2 = JObject.FromObject(person, serializer).ToString();
            Console.WriteLine(json1);
            Console.WriteLine(json2);
        }
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM