-CamelCasePropertyNamesContractResolver now updates dictionary and dynamic property names

This commit is contained in:
JamesNK
2011-03-21 08:06:12 +00:00
parent 71446e9772
commit 9e1c9e96fb
6 changed files with 75 additions and 1 deletions
@@ -167,6 +167,50 @@ namespace Newtonsoft.Json.Tests.Serialization
""Medium"", ""Medium"",
""Large"" ""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<string, string> values = new Dictionary<string, string>
{
{"First", "Value1!"},
{"Second", "Value2!"}
};
string json = JsonConvert.SerializeObject(values, Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Assert.AreEqual(@"{
""first"": ""Value1!"",
""second"": ""Value2!""
}", json); }", json);
} }
} }
@@ -8,6 +8,7 @@ using System.Linq.Expressions;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters;
using System.Text; using System.Text;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects; using Newtonsoft.Json.Tests.TestObjects;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using NUnit.Framework; using NUnit.Framework;
@@ -401,6 +401,8 @@ namespace Newtonsoft.Json.Serialization
JsonDictionaryContract contract = new JsonDictionaryContract(objectType); JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
InitializeContract(contract); InitializeContract(contract);
contract.PropertyNameResolver = ResolvePropertyName;
return contract; return contract;
} }
@@ -477,6 +479,7 @@ namespace Newtonsoft.Json.Serialization
JsonDynamicContract contract = new JsonDynamicContract(objectType); JsonDynamicContract contract = new JsonDynamicContract(objectType);
InitializeContract(contract); InitializeContract(contract);
contract.PropertyNameResolver = ResolvePropertyName;
contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut)); contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut));
return contract; return contract;
@@ -36,6 +36,12 @@ namespace Newtonsoft.Json.Serialization
/// </summary> /// </summary>
public class JsonDictionaryContract : JsonContract public class JsonDictionaryContract : JsonContract
{ {
/// <summary>
/// Gets or sets the property name resolver.
/// </summary>
/// <value>The property name resolver.</value>
public Func<string, string> PropertyNameResolver { get; set; }
internal Type DictionaryKeyType { get; private set; } internal Type DictionaryKeyType { get; private set; }
internal Type DictionaryValueType { get; private set; } internal Type DictionaryValueType { get; private set; }
@@ -43,6 +43,12 @@ namespace Newtonsoft.Json.Serialization
/// <value>The object's properties.</value> /// <value>The object's properties.</value>
public JsonPropertyCollection Properties { get; private set; } public JsonPropertyCollection Properties { get; private set; }
/// <summary>
/// Gets or sets the property name resolver.
/// </summary>
/// <value>The property name resolver.</value>
public Func<string, string> PropertyNameResolver { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JsonDynamicContract"/> class. /// Initializes a new instance of the <see cref="JsonDynamicContract"/> class.
/// </summary> /// </summary>
@@ -475,6 +475,12 @@ namespace Newtonsoft.Json.Serialization
#endif #endif
#if !(NET35 || NET20 || WINDOWS_PHONE) #if !(NET35 || NET20 || WINDOWS_PHONE)
/// <summary>
/// Serializes the dynamic.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="value">The value.</param>
/// <param name="contract">The contract.</param>
private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract) private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract)
{ {
contract.InvokeOnSerializing(value, Serializer.Context); contract.InvokeOnSerializing(value, Serializer.Context);
@@ -487,7 +493,11 @@ namespace Newtonsoft.Json.Serialization
object memberValue; object memberValue;
if (DynamicUtils.TryGetMember(value, memberName, out 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); SerializeValue(writer, memberValue, GetContractSafe(memberValue), null, null);
} }
} }
@@ -547,6 +557,10 @@ namespace Newtonsoft.Json.Serialization
{ {
string propertyName = GetPropertyName(entry); string propertyName = GetPropertyName(entry);
propertyName = (contract.PropertyNameResolver != null)
? contract.PropertyNameResolver(propertyName)
: propertyName;
try try
{ {
object value = entry.Value; object value = entry.Value;