diff options
author | JamesNK <james@newtonking.com> | 2011-03-21 11:06:12 +0300 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2011-03-21 11:06:12 +0300 |
commit | 9e1c9e96fb2d279ea24757a882505ba7e270449b (patch) | |
tree | ddc8ffb8e3bfa50573fc0551728e69480a80508f | |
parent | 71446e97722b3eecf2ea30f427547936eabaa31d (diff) |
-CamelCasePropertyNamesContractResolver now updates dictionary and dynamic property names
6 files changed, 75 insertions, 1 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs index 5e31d4b..b67a5cc 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs @@ -169,5 +169,49 @@ namespace Newtonsoft.Json.Tests.Serialization ]
}", 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);
+ }
}
}
\ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs index a13fdf9..f57f153 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 cfbdb51..dc23e38 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 4913b8c..6721970 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -36,6 +36,12 @@ namespace Newtonsoft.Json.Serialization /// </summary>
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 DictionaryValueType { get; private set; }
diff --git a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs index 007579e..75f08be 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs @@ -44,6 +44,12 @@ namespace Newtonsoft.Json.Serialization 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>
/// Initializes a new instance of the <see cref="JsonDynamicContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 706e17a..9e06fc6 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)
+ /// <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)
{
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;
|