Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Serialization\CamelCasePropertyNamesContractResolverTests.cs
Symbol Coverage: 100.00% (37 of 37)
Branch Coverage: 100.00% (5 of 5)
Cyclomatic Complexity Avg: 1.00 Max:1
Code Lines: 85
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person |
|
5 |
// obtaining a copy of this software and associated documentation |
|
6 |
// files (the "Software"), to deal in the Software without |
|
7 |
// restriction, including without limitation the rights to use, |
|
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
// copies of the Software, and to permit persons to whom the |
|
10 |
// Software is furnished to do so, subject to the following |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.Linq; |
|
29 |
using System.Text; |
|
30 |
using Newtonsoft.Json.Serialization; |
|
31 |
using NUnit.Framework; |
|
32 |
using Newtonsoft.Json.Tests.TestObjects; |
|
33 |
using Newtonsoft.Json.Linq; |
|
34 |
using System.Reflection; |
|
35 |
using Newtonsoft.Json.Utilities; |
|
36 |
||
37 |
namespace Newtonsoft.Json.Tests.Serialization |
|
38 |
{ |
|
39 |
public class CamelCasePropertyNamesContractResolverTests : TestFixtureBase |
|
40 |
{ |
|
41 |
[Test] |
|
42 |
public void JsonConvertSerializerSettings() |
|
43 |
{ |
|
44 |
1 |
Person person = new Person();
|
45 |
1 |
person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
|
46 |
1 |
person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
|
47 |
1 |
person.Name = "Name!";
|
48 |
||
49 |
1 |
string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
|
50 |
1 |
{
|
51 |
1 |
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
52 |
1 |
});
|
53 |
||
54 |
1 |
Assert.AreEqual(@"{
|
55 |
1 |
""name"": ""Name!"",
|
56 |
1 |
""birthDate"": ""\/Date(974764544000)\/"",
|
57 |
1 |
""lastModified"": ""\/Date(974764544000)\/""
|
58 |
1 |
}", json);
|
59 |
||
60 |
1 |
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings
|
61 |
1 |
{
|
62 |
1 |
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
63 |
1 |
});
|
64 |
||
65 |
1 |
Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
|
66 |
1 |
Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
|
67 |
1 |
Assert.AreEqual(person.Name, deserializedPerson.Name);
|
68 |
||
69 |
1 |
json = JsonConvert.SerializeObject(person, Formatting.Indented);
|
70 |
1 |
Assert.AreEqual(@"{
|
71 |
1 |
""Name"": ""Name!"",
|
72 |
1 |
""BirthDate"": ""\/Date(974764544000)\/"",
|
73 |
1 |
""LastModified"": ""\/Date(974764544000)\/""
|
74 |
1 |
}", json);
|
75 |
||
76 |
1 |
}
|
77 |
||
78 |
[Test] |
|
79 |
public void JTokenWriter() |
|
80 |
{ |
|
81 |
1 |
JsonIgnoreAttributeOnClassTestClass ignoreAttributeOnClassTestClass = new JsonIgnoreAttributeOnClassTestClass();
|
82 |
1 |
ignoreAttributeOnClassTestClass.Field = int.MinValue;
|
83 |
||
84 |
1 |
JsonSerializer serializer = new JsonSerializer();
|
85 |
1 |
serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
86 |
||
87 |
1 |
JTokenWriter writer = new JTokenWriter();
|
88 |
||
89 |
1 |
serializer.Serialize(writer, ignoreAttributeOnClassTestClass);
|
90 |
||
91 |
1 |
JObject o = (JObject) writer.Token;
|
92 |
1 |
JProperty p = o.Property("theField");
|
93 |
||
94 |
1 |
Assert.IsNotNull(p);
|
95 |
1 |
Assert.AreEqual(int.MinValue, (int)p.Value);
|
96 |
||
97 |
1 |
string json = o.ToString();
|
98 |
1 |
}
|
99 |
||
100 |
[Test] |
|
101 |
public void MemberSearchFlags() |
|
102 |
{ |
|
103 |
1 |
PrivateMembersClass privateMembersClass = new PrivateMembersClass("PrivateString!", "InternalString!");
|
104 |
||
105 |
1 |
string json = JsonConvert.SerializeObject(privateMembersClass, Formatting.Indented, new JsonSerializerSettings
|
106 |
1 |
{
|
107 |
1 |
ContractResolver = new CamelCasePropertyNamesContractResolver { DefaultMembersSearchFlags = BindingFlags.NonPublic | BindingFlags.Instance }
|
108 |
1 |
});
|
109 |
||
110 |
1 |
Assert.AreEqual(@"{
|
111 |
1 |
""_privateString"": ""PrivateString!"",
|
112 |
1 |
""i"": 0,
|
113 |
1 |
""_internalString"": ""InternalString!""
|
114 |
1 |
}", json);
|
115 |
||
116 |
1 |
PrivateMembersClass deserializedPrivateMembersClass = JsonConvert.DeserializeObject<PrivateMembersClass>(@"{
|
117 |
1 |
""_privateString"": ""Private!"",
|
118 |
1 |
""i"": -2,
|
119 |
1 |
""_internalString"": ""Internal!""
|
120 |
1 |
}", new JsonSerializerSettings
|
121 |
1 |
{
|
122 |
1 |
ContractResolver = new CamelCasePropertyNamesContractResolver { DefaultMembersSearchFlags = BindingFlags.NonPublic | BindingFlags.Instance }
|
123 |
1 |
});
|
124 |
||
125 |
1 |
Assert.AreEqual("Private!", ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("_privateString", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
|
126 |
1 |
Assert.AreEqual("Internal!", ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("_internalString", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
|
127 |
||
128 |
// readonly |
|
129 |
1 |
Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("i", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
|
130 |
1 |
}
|
131 |
||
132 |
[Test] |
|
133 |
public void BlogPostExample() |
|
134 |
{ |
|
135 |
1 |
Product product = new Product
|
136 |
1 |
{
|
137 |
1 |
ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc),
|
138 |
1 |
Name = "Widget",
|
139 |
1 |
Price = 9.99m,
|
140 |
1 |
Sizes = new[] {"Small", "Medium", "Large"}
|
141 |
1 |
};
|
142 |
||
143 |
1 |
string json =
|
144 |
1 |
JsonConvert.SerializeObject(
|
145 |
1 |
product,
|
146 |
1 |
Formatting.Indented,
|
147 |
1 |
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
|
148 |
1 |
);
|
149 |
||
150 |
//{ |
|
151 |
// "name": "Widget", |
|
152 |
// "expiryDate": "\/Date(1292868060000)\/", |
|
153 |
// "price": 9.99, |
|
154 |
// "sizes": [ |
|
155 |
// "Small", |
|
156 |
// "Medium", |
|
157 |
// "Large" |
|
158 |
// ] |
|
159 |
//} |
|
160 |
||
161 |
1 |
Assert.AreEqual(@"{
|
162 |
1 |
""name"": ""Widget"",
|
163 |
1 |
""expiryDate"": ""\/Date(1292868060000)\/"",
|
164 |
1 |
""price"": 9.99,
|
165 |
1 |
""sizes"": [
|
166 |
1 |
""Small"",
|
167 |
1 |
""Medium"",
|
168 |
1 |
""Large""
|
169 |
1 |
]
|
170 |
1 |
}", json);
|
171 |
1 |
}
|
172 |
} |
|
173 |
} |