From 0929c2b4c3349ae1fb3d22c522504fbf57c8268e Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 3 May 2015 16:56:41 +1200 Subject: [PATCH] -Added DefaultContractResolver.ResolveDictionaryKey -Added JsonDictionaryContract.DictionaryKeyResolver -Marked JsonDictionaryContract.PropertyNameResolver as obsolete --- .../Properties/AssemblyInfo.cs | 2 +- .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/JsonSerializerTest.cs | 42 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/DefaultContractResolver.cs | 14 ++++++- .../Serialization/JsonDictionaryContract.cs | 13 +++++- .../JsonSerializerInternalWriter.cs | 4 +- 7 files changed, 71 insertions(+), 8 deletions(-) diff --git a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs index 17efc85f..0d44e57a 100644 --- a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs @@ -57,4 +57,4 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("7.0.0.0")] -[assembly: AssemblyFileVersion("7.0.1.18502")] +[assembly: AssemblyFileVersion("7.0.1.18503")] diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 79c8d30e..315fbfb6 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -76,4 +76,4 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("7.0.0.0")] -[assembly: AssemblyFileVersion("7.0.1.18502")] +[assembly: AssemblyFileVersion("7.0.1.18503")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 87eb00d6..86c57588 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -95,6 +95,48 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class JsonSerializerTest : TestFixtureBase { + public class DictionaryKeyContractResolver : DefaultContractResolver + { + protected internal override string ResolveDictionaryKey(string dictionaryKey) + { + return dictionaryKey; + } + + protected internal override string ResolvePropertyName(string propertyName) + { + return propertyName.ToUpper(CultureInfo.InvariantCulture); + } + } + + [Test] + public void DictionaryKeyContractResolverTest() + { + var person = new + { + Name = "James", + Age = 1, + RoleNames = new Dictionary + { + { "IsAdmin", true }, + { "IsModerator", false } + } + }; + + string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings + { + ContractResolver = new DictionaryKeyContractResolver() + }); + + Assert.AreEqual(@"{ + ""NAME"": ""James"", + ""AGE"": 1, + ""ROLENAMES"": { + ""IsAdmin"": true, + ""IsModerator"": false + } +}", json); + } + [Test] public void IncompleteContainers() { diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index fa5f8396..9da0f2f2 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -92,5 +92,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("7.0.0.0")] -[assembly: AssemblyFileVersion("7.0.1.18502")] +[assembly: AssemblyFileVersion("7.0.1.18503")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index c8316f47..ef35646a 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -880,7 +880,7 @@ namespace Newtonsoft.Json.Serialization JsonDictionaryContract contract = new JsonDictionaryContract(objectType); InitializeContract(contract); - contract.PropertyNameResolver = ResolvePropertyName; + contract.DictionaryKeyResolver = ResolveDictionaryKey; return contract; } @@ -1360,12 +1360,22 @@ namespace Newtonsoft.Json.Serialization /// Resolves the name of the property. /// /// Name of the property. - /// Name of the property. + /// Resolved name of the property. protected internal virtual string ResolvePropertyName(string propertyName) { return propertyName; } + /// + /// Resolves the key of the dictionary. By default is used to resolve dictionary keys. + /// + /// Key of the dictionary. + /// Resolved key of the dictionary. + protected internal virtual string ResolveDictionaryKey(string dictionaryKey) + { + return ResolvePropertyName(dictionaryKey); + } + /// /// Gets the resolved name of the property. /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index 191fec2f..693150f3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -45,7 +45,18 @@ namespace Newtonsoft.Json.Serialization /// Gets or sets the property name resolver. /// /// The property name resolver. - public Func PropertyNameResolver { get; set; } + [Obsolete("PropertyNameResolver is obsolete. Use DictionaryKeyResolver instead.")] + public Func PropertyNameResolver + { + get { return DictionaryKeyResolver; } + set { DictionaryKeyResolver = value; } + } + + /// + /// Gets or sets the dictionary key resolver. + /// + /// The dictionary key resolver. + public Func DictionaryKeyResolver { get; set; } /// /// Gets the of the dictionary keys. diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 1728b7c0..743a18df 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -921,8 +921,8 @@ namespace Newtonsoft.Json.Serialization bool escape; string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape); - propertyName = (contract.PropertyNameResolver != null) - ? contract.PropertyNameResolver(propertyName) + propertyName = (contract.DictionaryKeyResolver != null) + ? contract.DictionaryKeyResolver(propertyName) : propertyName; try