2 // Copyright (c) 2007 James Newton-King
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
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
27 using System.Collections.Generic;
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;
37 namespace Newtonsoft.Json.Tests.Serialization
39 public class CamelCasePropertyNamesContractResolverTests : TestFixtureBase
42 public void JsonConvertSerializerSettings()
44 Person person = new Person();
45 person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
46 person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
47 person.Name = "Name!";
49 string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
51 ContractResolver = new CamelCasePropertyNamesContractResolver()
56 ""birthDate"": ""\/Date(974764544000)\/"",
57 ""lastModified"": ""\/Date(974764544000)\/""
60 Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings
62 ContractResolver = new CamelCasePropertyNamesContractResolver()
65 Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
66 Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
67 Assert.AreEqual(person.Name, deserializedPerson.Name);
69 json = JsonConvert.SerializeObject(person, Formatting.Indented);
72 ""BirthDate"": ""\/Date(974764544000)\/"",
73 ""LastModified"": ""\/Date(974764544000)\/""
79 public void JTokenWriter()
81 JsonIgnoreAttributeOnClassTestClass ignoreAttributeOnClassTestClass = new JsonIgnoreAttributeOnClassTestClass();
82 ignoreAttributeOnClassTestClass.Field = int.MinValue;
84 JsonSerializer serializer = new JsonSerializer();
85 serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
87 JTokenWriter writer = new JTokenWriter();
89 serializer.Serialize(writer, ignoreAttributeOnClassTestClass);
91 JObject o = (JObject) writer.Token;
92 JProperty p = o.Property("theField");
95 Assert.AreEqual(int.MinValue, (int)p.Value);
97 string json = o.ToString();
101 public void MemberSearchFlags()
103 PrivateMembersClass privateMembersClass = new PrivateMembersClass("PrivateString!", "InternalString!");
105 string json = JsonConvert.SerializeObject(privateMembersClass, Formatting.Indented, new JsonSerializerSettings
107 ContractResolver = new CamelCasePropertyNamesContractResolver { DefaultMembersSearchFlags = BindingFlags.NonPublic | BindingFlags.Instance }
111 ""_privateString"": ""PrivateString!"",
113 ""_internalString"": ""InternalString!""
116 PrivateMembersClass deserializedPrivateMembersClass = JsonConvert.DeserializeObject<PrivateMembersClass>(@"{
117 ""_privateString"": ""Private!"",
119 ""_internalString"": ""Internal!""
120 }", new JsonSerializerSettings
122 ContractResolver = new CamelCasePropertyNamesContractResolver { DefaultMembersSearchFlags = BindingFlags.NonPublic | BindingFlags.Instance }
125 Assert.AreEqual("Private!", ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("_privateString", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
126 Assert.AreEqual("Internal!", ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("_internalString", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
129 Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(PrivateMembersClass).GetField("i", BindingFlags.Instance | BindingFlags.NonPublic), deserializedPrivateMembersClass));
133 public void BlogPostExample()
135 Product product = new Product
137 ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc),
140 Sizes = new[] {"Small", "Medium", "Large"}
144 JsonConvert.SerializeObject(
147 new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
152 // "expiryDate": "\/Date(1292868060000)\/",
162 ""name"": ""Widget"",
163 ""expiryDate"": ""\/Date(1292868060000)\/"",
173 #if !(NET35 || NET20 || WINDOWS_PHONE)
175 public void DynamicCamelCasePropertyNames()
177 dynamic o = new DynamicTests.TestDynamicObject();
179 o.Integer = int.MaxValue;
181 string json = JsonConvert.SerializeObject(o, Formatting.Indented,
182 new JsonSerializerSettings
184 ContractResolver = new CamelCasePropertyNamesContractResolver()
189 ""integer"": 2147483647,
191 ""childObject"": null
197 public void DictionaryCamelCasePropertyNames()
199 Dictionary<string, string> values = new Dictionary<string, string>
201 {"First", "Value1!"},
202 {"Second", "Value2!"}
205 string json = JsonConvert.SerializeObject(values, Formatting.Indented,
206 new JsonSerializerSettings
208 ContractResolver = new CamelCasePropertyNamesContractResolver()
212 ""first"": ""Value1!"",
213 ""second"": ""Value2!""