From 9e1c9e96fb2d279ea24757a882505ba7e270449b Mon Sep 17 00:00:00 2001 From: JamesNK Date: Mon, 21 Mar 2011 08:06:12 +0000 Subject: [PATCH] -CamelCasePropertyNamesContractResolver now updates dictionary and dynamic property names --- ...lCasePropertyNamesContractResolverTests.cs | 44 +++++++++++++++++++ .../Serialization/DynamicTests.cs | 1 + .../Serialization/DefaultContractResolver.cs | 3 ++ .../Serialization/JsonDictionaryContract.cs | 6 +++ .../Serialization/JsonDynamicContract.cs | 6 +++ .../JsonSerializerInternalWriter.cs | 16 ++++++- 6 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs index 5e31d4b5..b67a5cc3 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs @@ -167,6 +167,50 @@ namespace Newtonsoft.Json.Tests.Serialization ""Medium"", ""Large"" ] +}", json); + } + +#if !(NET35 || NET20 || WINDOWS_PHONE) + [Test] + public void DynamicCamelCasePropertyNames() + { + dynamic o = new DynamicTests.TestDynamicObject(); + o.Text = "Text!"; + o.Integer = int.MaxValue; + + string json = JsonConvert.SerializeObject(o, Formatting.Indented, + new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + + Assert.AreEqual(@"{ + ""text"": ""Text!"", + ""integer"": 2147483647, + ""int"": 0, + ""childObject"": null +}", json); + } +#endif + + [Test] + public void DictionaryCamelCasePropertyNames() + { + Dictionary values = new Dictionary + { + {"First", "Value1!"}, + {"Second", "Value2!"} + }; + + string json = JsonConvert.SerializeObject(values, Formatting.Indented, + new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + + Assert.AreEqual(@"{ + ""first"": ""Value1!"", + ""second"": ""Value2!"" }", json); } } diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs index a13fdf94..f57f1536 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs @@ -8,6 +8,7 @@ using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Runtime.Serialization.Formatters; using System.Text; +using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Tests.TestObjects; using Newtonsoft.Json.Utilities; using NUnit.Framework; diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index cfbdb510..dc23e38a 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -401,6 +401,8 @@ namespace Newtonsoft.Json.Serialization JsonDictionaryContract contract = new JsonDictionaryContract(objectType); InitializeContract(contract); + contract.PropertyNameResolver = ResolvePropertyName; + return contract; } @@ -477,6 +479,7 @@ namespace Newtonsoft.Json.Serialization JsonDynamicContract contract = new JsonDynamicContract(objectType); InitializeContract(contract); + contract.PropertyNameResolver = ResolvePropertyName; contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut)); return contract; diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index 4913b8cd..67219703 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -36,6 +36,12 @@ namespace Newtonsoft.Json.Serialization /// public class JsonDictionaryContract : JsonContract { + /// + /// Gets or sets the property name resolver. + /// + /// The property name resolver. + public Func PropertyNameResolver { get; set; } + internal Type DictionaryKeyType { get; private set; } internal Type DictionaryValueType { get; private set; } diff --git a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs index 007579e3..75f08bea 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs @@ -43,6 +43,12 @@ namespace Newtonsoft.Json.Serialization /// The object's properties. public JsonPropertyCollection Properties { get; private set; } + /// + /// Gets or sets the property name resolver. + /// + /// The property name resolver. + public Func PropertyNameResolver { get; set; } + /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 706e17ab..9e06fc6c 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -475,6 +475,12 @@ namespace Newtonsoft.Json.Serialization #endif #if !(NET35 || NET20 || WINDOWS_PHONE) + /// + /// Serializes the dynamic. + /// + /// The writer. + /// The value. + /// The contract. private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract) { contract.InvokeOnSerializing(value, Serializer.Context); @@ -487,7 +493,11 @@ namespace Newtonsoft.Json.Serialization object memberValue; if (DynamicUtils.TryGetMember(value, memberName, out memberValue)) { - writer.WritePropertyName(memberName); + string resolvedPropertyName = (contract.PropertyNameResolver != null) + ? contract.PropertyNameResolver(memberName) + : memberName; + + writer.WritePropertyName(resolvedPropertyName); SerializeValue(writer, memberValue, GetContractSafe(memberValue), null, null); } } @@ -547,6 +557,10 @@ namespace Newtonsoft.Json.Serialization { string propertyName = GetPropertyName(entry); + propertyName = (contract.PropertyNameResolver != null) + ? contract.PropertyNameResolver(propertyName) + : propertyName; + try { object value = entry.Value;