CustomContractResolver.cs 838 B

12345678910111213141516171819202122232425
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Serialization;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace MS.WebCore.Core
  8. {
  9. public class CustomContractResolver : DefaultContractResolver
  10. {
  11. private readonly HashSet<string> _propertiesToSerialize;
  12. public CustomContractResolver(IEnumerable<string> propertiesToSerialize)
  13. {
  14. _propertiesToSerialize = new HashSet<string>(propertiesToSerialize);
  15. }
  16. protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
  17. {
  18. IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
  19. return properties.Where(p => _propertiesToSerialize.Contains(p.PropertyName)).ToList();
  20. }
  21. }
  22. }