-Added DefaultContractResolver.ResolveDictionaryKey

-Added JsonDictionaryContract.DictionaryKeyResolver
-Marked JsonDictionaryContract.PropertyNameResolver as obsolete
This commit is contained in:
James Newton-King
2015-05-03 16:56:41 +12:00
parent 841464d014
commit 0929c2b4c3
7 changed files with 71 additions and 8 deletions
@@ -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")]
@@ -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")]
@@ -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<string, bool>
{
{ "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()
{
@@ -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)]
@@ -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.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>Name of the property.</returns>
/// <returns>Resolved name of the property.</returns>
protected internal virtual string ResolvePropertyName(string propertyName)
{
return propertyName;
}
/// <summary>
/// Resolves the key of the dictionary. By default <see cref="M:ResolvePropertyName"/> is used to resolve dictionary keys.
/// </summary>
/// <param name="dictionaryKey">Key of the dictionary.</param>
/// <returns>Resolved key of the dictionary.</returns>
protected internal virtual string ResolveDictionaryKey(string dictionaryKey)
{
return ResolvePropertyName(dictionaryKey);
}
/// <summary>
/// Gets the resolved name of the property.
/// </summary>
@@ -45,7 +45,18 @@ namespace Newtonsoft.Json.Serialization
/// Gets or sets the property name resolver.
/// </summary>
/// <value>The property name resolver.</value>
public Func<string, string> PropertyNameResolver { get; set; }
[Obsolete("PropertyNameResolver is obsolete. Use DictionaryKeyResolver instead.")]
public Func<string, string> PropertyNameResolver
{
get { return DictionaryKeyResolver; }
set { DictionaryKeyResolver = value; }
}
/// <summary>
/// Gets or sets the dictionary key resolver.
/// </summary>
/// <value>The dictionary key resolver.</value>
public Func<string, string> DictionaryKeyResolver { get; set; }
/// <summary>
/// Gets the <see cref="Type"/> of the dictionary keys.
@@ -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