-Added DefaultContractResolver.ResolveDictionaryKey
-Added JsonDictionaryContract.DictionaryKeyResolver -Marked JsonDictionaryContract.PropertyNameResolver as obsolete
This commit is contained in:
@@ -57,4 +57,4 @@ using System.Runtime.InteropServices;
|
|||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[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:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[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]
|
[TestFixture]
|
||||||
public class JsonSerializerTest : TestFixtureBase
|
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]
|
[Test]
|
||||||
public void IncompleteContainers()
|
public void IncompleteContainers()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -92,5 +92,5 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("7.0.0.0")]
|
[assembly: AssemblyVersion("7.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("7.0.1.18502")]
|
[assembly: AssemblyFileVersion("7.0.1.18503")]
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -880,7 +880,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
|
JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
|
||||||
InitializeContract(contract);
|
InitializeContract(contract);
|
||||||
|
|
||||||
contract.PropertyNameResolver = ResolvePropertyName;
|
contract.DictionaryKeyResolver = ResolveDictionaryKey;
|
||||||
|
|
||||||
return contract;
|
return contract;
|
||||||
}
|
}
|
||||||
@@ -1360,12 +1360,22 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// Resolves the name of the property.
|
/// Resolves the name of the property.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="propertyName">Name of the property.</param>
|
/// <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)
|
protected internal virtual string ResolvePropertyName(string propertyName)
|
||||||
{
|
{
|
||||||
return 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>
|
/// <summary>
|
||||||
/// Gets the resolved name of the property.
|
/// Gets the resolved name of the property.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -45,7 +45,18 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
/// Gets or sets the property name resolver.
|
/// Gets or sets the property name resolver.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The property name resolver.</value>
|
/// <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>
|
/// <summary>
|
||||||
/// Gets the <see cref="Type"/> of the dictionary keys.
|
/// Gets the <see cref="Type"/> of the dictionary keys.
|
||||||
|
|||||||
@@ -921,8 +921,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
bool escape;
|
bool escape;
|
||||||
string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);
|
string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);
|
||||||
|
|
||||||
propertyName = (contract.PropertyNameResolver != null)
|
propertyName = (contract.DictionaryKeyResolver != null)
|
||||||
? contract.PropertyNameResolver(propertyName)
|
? contract.DictionaryKeyResolver(propertyName)
|
||||||
: propertyName;
|
: propertyName;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user