2 using System.Collections.Generic;
6 using Newtonsoft.Json.Serialization;
7 using Newtonsoft.Json.Tests.TestObjects;
8 using System.Reflection;
10 namespace Newtonsoft.Json.Tests.Serialization
12 public class DynamicContractResolver : DefaultContractResolver
14 private readonly char _startingWithChar;
15 public DynamicContractResolver(char startingWithChar)
18 _startingWithChar = startingWithChar;
21 protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
23 IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
25 // only serializer properties that start with the specified character
27 properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
35 public string BookName { get; set; }
36 public decimal BookPrice { get; set; }
37 public string AuthorName { get; set; }
38 public int AuthorAge { get; set; }
39 public string AuthorCountry { get; set; }
42 public interface IPerson
44 string FirstName { get; set; }
45 string LastName { get; set; }
46 DateTime BirthDate { get; set; }
49 public class Employee : IPerson
51 public string FirstName { get; set; }
52 public string LastName { get; set; }
53 public DateTime BirthDate { get; set; }
55 public string Department { get; set; }
56 public string JobTitle { get; set; }
59 public class IPersonContractResolver : DefaultContractResolver
61 protected override JsonContract CreateContract(Type objectType)
63 if (objectType == typeof(Employee))
64 objectType = typeof(IPerson);
66 return base.CreateContract(objectType);
70 public class ContractResolverTests : TestFixtureBase
73 public void SerializeInterface()
75 Employee employee = new Employee
77 BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
78 FirstName = "Maurice",
84 string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
85 new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
88 ""FirstName"": ""Maurice"",
89 ""LastName"": ""Moss"",
90 ""BirthDate"": ""\/Date(252291661000)\/""
95 public void SingleTypeWithMultipleContractResolvers()
99 BookName = "The Gathering Storm",
101 AuthorName = "Brandon Sanderson",
103 AuthorCountry = "United States of America"
106 string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
107 new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
110 // "AuthorName": "Brandon Sanderson",
112 // "AuthorCountry": "United States of America"
115 string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
116 new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
119 // "BookName": "The Gathering Storm",
120 // "BookPrice": 16.19
124 ""AuthorName"": ""Brandon Sanderson"",
126 ""AuthorCountry"": ""United States of America""
130 ""BookName"": ""The Gathering Storm"",
136 public void SerializeCompilerGeneratedMembers()
138 StructTest structTest = new StructTest
142 StringField = "Field",
143 StringProperty = "Property"
146 DefaultContractResolver skipCompilerGeneratedResolver = new DefaultContractResolver
148 DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
151 string skipCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
152 new JsonSerializerSettings { ContractResolver = skipCompilerGeneratedResolver });
155 ""StringField"": ""Field"",
157 ""StringProperty"": ""Property"",
159 }", skipCompilerGeneratedJson);
161 DefaultContractResolver includeCompilerGeneratedResolver = new DefaultContractResolver
163 DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
164 SerializeCompilerGeneratedMembers = true
167 string includeCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
168 new JsonSerializerSettings { ContractResolver = includeCompilerGeneratedResolver });
171 ""StringField"": ""Field"",
173 ""<StringProperty>k__BackingField"": ""Property"",
174 ""<IntProperty>k__BackingField"": 2,
175 ""StringProperty"": ""Property"",
177 }", includeCompilerGeneratedJson);