All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Serialization / ContractResolverTests.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using NUnit.Framework;
6 using Newtonsoft.Json.Serialization;
7 using Newtonsoft.Json.Tests.TestObjects;
8 using System.Reflection;
9
10 namespace Newtonsoft.Json.Tests.Serialization
11 {
12   public class DynamicContractResolver : DefaultContractResolver
13   {
14     private readonly char _startingWithChar;
15     public DynamicContractResolver(char startingWithChar)
16       : base(false)
17     {
18       _startingWithChar = startingWithChar;
19     }
20
21     protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
22     {
23       IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
24
25       // only serializer properties that start with the specified character
26       properties =
27         properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
28
29       return properties;
30     }
31   }
32
33   public class Book
34   {
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; }
40   }
41
42   public interface IPerson
43   {
44     string FirstName { get; set; }
45     string LastName { get; set; }
46     DateTime BirthDate { get; set; }
47   }
48
49   public class Employee : IPerson
50   {
51     public string FirstName { get; set; }
52     public string LastName { get; set; }
53     public DateTime BirthDate { get; set; }
54
55     public string Department { get; set; }
56     public string JobTitle { get; set; }
57   }
58
59   public class IPersonContractResolver : DefaultContractResolver
60   {
61     protected override JsonContract CreateContract(Type objectType)
62     {
63       if (objectType == typeof(Employee))
64         objectType = typeof(IPerson);
65
66       return base.CreateContract(objectType);
67     }
68   }
69
70   public class ContractResolverTests : TestFixtureBase
71   {
72     [Test]
73     public void SerializeInterface()
74     {
75       Employee employee = new Employee
76          {
77            BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
78            FirstName = "Maurice",
79            LastName = "Moss",
80            Department = "IT",
81            JobTitle = "Support"
82          };
83
84       string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
85         new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
86
87       Assert.AreEqual(@"{
88   ""FirstName"": ""Maurice"",
89   ""LastName"": ""Moss"",
90   ""BirthDate"": ""\/Date(252291661000)\/""
91 }", iPersonJson);
92     }
93
94     [Test]
95     public void SingleTypeWithMultipleContractResolvers()
96     {
97       Book book = new Book
98                     {
99                       BookName = "The Gathering Storm",
100                       BookPrice = 16.19m,
101                       AuthorName = "Brandon Sanderson",
102                       AuthorAge = 34,
103                       AuthorCountry = "United States of America"
104                     };
105
106       string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
107         new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
108
109       // {
110       //   "AuthorName": "Brandon Sanderson",
111       //   "AuthorAge": 34,
112       //   "AuthorCountry": "United States of America"
113       // }
114
115       string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
116         new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
117
118       // {
119       //   "BookName": "The Gathering Storm",
120       //   "BookPrice": 16.19
121       // }
122
123       Assert.AreEqual(@"{
124   ""AuthorName"": ""Brandon Sanderson"",
125   ""AuthorAge"": 34,
126   ""AuthorCountry"": ""United States of America""
127 }", startingWithA);
128
129       Assert.AreEqual(@"{
130   ""BookName"": ""The Gathering Storm"",
131   ""BookPrice"": 16.19
132 }", startingWithB);
133     }
134
135     [Test]
136     public void SerializeCompilerGeneratedMembers()
137     {
138       StructTest structTest = new StructTest
139         {
140           IntField = 1,
141           IntProperty = 2,
142           StringField = "Field",
143           StringProperty = "Property"
144         };
145
146       DefaultContractResolver skipCompilerGeneratedResolver = new DefaultContractResolver
147       {
148         DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
149       };
150
151       string skipCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
152         new JsonSerializerSettings { ContractResolver = skipCompilerGeneratedResolver });
153
154       Assert.AreEqual(@"{
155   ""StringField"": ""Field"",
156   ""IntField"": 1,
157   ""StringProperty"": ""Property"",
158   ""IntProperty"": 2
159 }", skipCompilerGeneratedJson);
160
161       DefaultContractResolver includeCompilerGeneratedResolver = new DefaultContractResolver
162       {
163         DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
164         SerializeCompilerGeneratedMembers = true
165       };
166
167       string includeCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
168         new JsonSerializerSettings { ContractResolver = includeCompilerGeneratedResolver });
169
170       Assert.AreEqual(@"{
171   ""StringField"": ""Field"",
172   ""IntField"": 1,
173   ""<StringProperty>k__BackingField"": ""Property"",
174   ""<IntProperty>k__BackingField"": 2,
175   ""StringProperty"": ""Property"",
176   ""IntProperty"": 2
177 }", includeCompilerGeneratedJson);
178     }
179   }
180 }