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 Newtonsoft.Json.Serialization; 29 #if NETFX_CORE 30 using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 31 using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; 32 using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; 33 #elif DNXCORE50 34 using Xunit; 35 using Test = Xunit.FactAttribute; 36 using Assert = Newtonsoft.Json.Tests.XUnitAssert; 37 #else 38 using NUnit.Framework; 39 #endif 40 using Newtonsoft.Json.Tests.TestObjects; 41 using Newtonsoft.Json.Linq; 42 using System.Reflection; 43 using Newtonsoft.Json.Utilities; 44 45 namespace Newtonsoft.Json.Tests.Serialization 46 { 47 [TestFixture] 48 public class CamelCaseNamingStrategyTests : TestFixtureBase 49 { 50 [Test] JsonConvertSerializerSettings()51 public void JsonConvertSerializerSettings() 52 { 53 Person person = new Person(); 54 person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc); 55 person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc); 56 person.Name = "Name!"; 57 58 DefaultContractResolver contractResolver = new DefaultContractResolver 59 { 60 NamingStrategy = new CamelCaseNamingStrategy() 61 }; 62 63 string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings 64 { 65 ContractResolver = contractResolver 66 }); 67 68 StringAssert.AreEqual(@"{ 69 ""name"": ""Name!"", 70 ""birthDate"": ""2000-11-20T23:55:44Z"", 71 ""lastModified"": ""2000-11-20T23:55:44Z"" 72 }", json); 73 74 Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings 75 { 76 ContractResolver = contractResolver 77 }); 78 79 Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate); 80 Assert.AreEqual(person.LastModified, deserializedPerson.LastModified); 81 Assert.AreEqual(person.Name, deserializedPerson.Name); 82 83 json = JsonConvert.SerializeObject(person, Formatting.Indented); 84 StringAssert.AreEqual(@"{ 85 ""Name"": ""Name!"", 86 ""BirthDate"": ""2000-11-20T23:55:44Z"", 87 ""LastModified"": ""2000-11-20T23:55:44Z"" 88 }", json); 89 } 90 91 [Test] JTokenWriter_OverrideSpecifiedName()92 public void JTokenWriter_OverrideSpecifiedName() 93 { 94 JsonIgnoreAttributeOnClassTestClass ignoreAttributeOnClassTestClass = new JsonIgnoreAttributeOnClassTestClass(); 95 ignoreAttributeOnClassTestClass.Field = int.MinValue; 96 97 DefaultContractResolver contractResolver = new DefaultContractResolver 98 { 99 NamingStrategy = new CamelCaseNamingStrategy 100 { 101 OverrideSpecifiedNames = true 102 } 103 }; 104 105 JsonSerializer serializer = new JsonSerializer(); 106 serializer.ContractResolver = contractResolver; 107 108 JTokenWriter writer = new JTokenWriter(); 109 110 serializer.Serialize(writer, ignoreAttributeOnClassTestClass); 111 112 JObject o = (JObject)writer.Token; 113 JProperty p = o.Property("theField"); 114 115 Assert.IsNotNull(p); 116 Assert.AreEqual(int.MinValue, (int)p.Value); 117 } 118 119 [Test] BlogPostExample()120 public void BlogPostExample() 121 { 122 Product product = new Product 123 { 124 ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc), 125 Name = "Widget", 126 Price = 9.99m, 127 Sizes = new[] { "Small", "Medium", "Large" } 128 }; 129 130 DefaultContractResolver contractResolver = new DefaultContractResolver 131 { 132 NamingStrategy = new CamelCaseNamingStrategy() 133 }; 134 135 string json = 136 JsonConvert.SerializeObject( 137 product, 138 Formatting.Indented, 139 new JsonSerializerSettings { ContractResolver = contractResolver } 140 ); 141 142 //{ 143 // "name": "Widget", 144 // "expiryDate": "\/Date(1292868060000)\/", 145 // "price": 9.99, 146 // "sizes": [ 147 // "Small", 148 // "Medium", 149 // "Large" 150 // ] 151 //} 152 153 StringAssert.AreEqual(@"{ 154 ""name"": ""Widget"", 155 ""expiryDate"": ""2010-12-20T18:01:00Z"", 156 ""price"": 9.99, 157 ""sizes"": [ 158 ""Small"", 159 ""Medium"", 160 ""Large"" 161 ] 162 }", json); 163 } 164 165 #if !(NET35 || NET20 || PORTABLE40) 166 [Test] DynamicCamelCasePropertyNames()167 public void DynamicCamelCasePropertyNames() 168 { 169 dynamic o = new TestDynamicObject(); 170 o.Text = "Text!"; 171 o.Integer = int.MaxValue; 172 173 DefaultContractResolver contractResolver = new DefaultContractResolver 174 { 175 NamingStrategy = new CamelCaseNamingStrategy 176 { 177 ProcessDictionaryKeys = true 178 } 179 }; 180 181 string json = JsonConvert.SerializeObject(o, Formatting.Indented, 182 new JsonSerializerSettings 183 { 184 ContractResolver = contractResolver 185 }); 186 187 StringAssert.AreEqual(@"{ 188 ""explicit"": false, 189 ""text"": ""Text!"", 190 ""integer"": 2147483647, 191 ""int"": 0, 192 ""childObject"": null 193 }", json); 194 } 195 #endif 196 197 [Test] DictionaryCamelCasePropertyNames_Disabled()198 public void DictionaryCamelCasePropertyNames_Disabled() 199 { 200 Dictionary<string, string> values = new Dictionary<string, string> 201 { 202 { "First", "Value1!" }, 203 { "Second", "Value2!" } 204 }; 205 206 DefaultContractResolver contractResolver = new DefaultContractResolver 207 { 208 NamingStrategy = new CamelCaseNamingStrategy() 209 }; 210 211 string json = JsonConvert.SerializeObject(values, Formatting.Indented, 212 new JsonSerializerSettings 213 { 214 ContractResolver = contractResolver 215 }); 216 217 StringAssert.AreEqual(@"{ 218 ""First"": ""Value1!"", 219 ""Second"": ""Value2!"" 220 }", json); 221 } 222 223 [Test] DictionaryCamelCasePropertyNames_Enabled()224 public void DictionaryCamelCasePropertyNames_Enabled() 225 { 226 Dictionary<string, string> values = new Dictionary<string, string> 227 { 228 { "First", "Value1!" }, 229 { "Second", "Value2!" } 230 }; 231 232 DefaultContractResolver contractResolver = new DefaultContractResolver 233 { 234 NamingStrategy = new CamelCaseNamingStrategy 235 { 236 ProcessDictionaryKeys = true 237 } 238 }; 239 240 string json = JsonConvert.SerializeObject(values, Formatting.Indented, 241 new JsonSerializerSettings 242 { 243 ContractResolver = contractResolver 244 }); 245 246 StringAssert.AreEqual(@"{ 247 ""first"": ""Value1!"", 248 ""second"": ""Value2!"" 249 }", json); 250 } 251 252 public class PropertyAttributeNamingStrategyTestClass 253 { 254 [JsonProperty] 255 public string HasNoAttributeNamingStrategy { get; set; } 256 257 [JsonProperty(NamingStrategyType = typeof(CamelCaseNamingStrategy))] 258 public string HasAttributeNamingStrategy { get; set; } 259 } 260 261 [Test] JsonPropertyAttribute_NamingStrategyType()262 public void JsonPropertyAttribute_NamingStrategyType() 263 { 264 PropertyAttributeNamingStrategyTestClass c = new PropertyAttributeNamingStrategyTestClass 265 { 266 HasNoAttributeNamingStrategy = "Value1!", 267 HasAttributeNamingStrategy = "Value2!" 268 }; 269 270 string json = JsonConvert.SerializeObject(c, Formatting.Indented); 271 272 StringAssert.AreEqual(@"{ 273 ""HasNoAttributeNamingStrategy"": ""Value1!"", 274 ""hasAttributeNamingStrategy"": ""Value2!"" 275 }", json); 276 } 277 278 [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] 279 public class ContainerAttributeNamingStrategyTestClass 280 { 281 public string Prop1 { get; set; } 282 public string Prop2 { get; set; } 283 [JsonProperty(NamingStrategyType = typeof(DefaultNamingStrategy))] 284 public string HasAttributeNamingStrategy { get; set; } 285 } 286 287 [Test] JsonObjectAttribute_NamingStrategyType()288 public void JsonObjectAttribute_NamingStrategyType() 289 { 290 ContainerAttributeNamingStrategyTestClass c = new ContainerAttributeNamingStrategyTestClass 291 { 292 Prop1 = "Value1!", 293 Prop2 = "Value2!" 294 }; 295 296 string json = JsonConvert.SerializeObject(c, Formatting.Indented); 297 298 StringAssert.AreEqual(@"{ 299 ""prop1"": ""Value1!"", 300 ""prop2"": ""Value2!"", 301 ""HasAttributeNamingStrategy"": null 302 }", json); 303 } 304 305 [JsonDictionary(NamingStrategyType = typeof(CamelCaseNamingStrategy), NamingStrategyParameters = new object[] { true, true })] 306 public class DictionaryAttributeNamingStrategyTestClass : Dictionary<string, string> 307 { 308 } 309 310 [Test] JsonDictionaryAttribute_NamingStrategyType()311 public void JsonDictionaryAttribute_NamingStrategyType() 312 { 313 DictionaryAttributeNamingStrategyTestClass c = new DictionaryAttributeNamingStrategyTestClass 314 { 315 ["Key1"] = "Value1!", 316 ["Key2"] = "Value2!" 317 }; 318 319 string json = JsonConvert.SerializeObject(c, Formatting.Indented); 320 321 StringAssert.AreEqual(@"{ 322 ""key1"": ""Value1!"", 323 ""key2"": ""Value2!"" 324 }", json); 325 } 326 } 327 }