-Fixed KeyValuePair case insensitive deserializing
-Fixed KeyValuePair not using contract resolver for property names when serializing
This commit is contained in:
@@ -72,5 +72,5 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.6.15009")]
|
[assembly: AssemblyFileVersion("4.5.7.15030")]
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6496,6 +6496,33 @@ Parameter name: value",
|
|||||||
var actual = JsonConvert.DeserializeObject<MyType>(@"{ ""MyProperty"":{""Key"":""Y""}}");
|
var actual = JsonConvert.DeserializeObject<MyType>(@"{ ""MyProperty"":{""Key"":""Y""}}");
|
||||||
Assert.AreEqual("X", actual.MyProperty["Key"]);
|
Assert.AreEqual("X", actual.MyProperty["Key"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeCaseInsensitiveKeyValuePairConverter()
|
||||||
|
{
|
||||||
|
KeyValuePair<int, string> result =
|
||||||
|
JsonConvert.DeserializeObject<KeyValuePair<int, string>>(
|
||||||
|
"{key: 123, \"VALUE\": \"test value\"}"
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.AreEqual(123, result.Key);
|
||||||
|
Assert.AreEqual("test value", result.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeKeyValuePairConverterWithCamelCase()
|
||||||
|
{
|
||||||
|
string json =
|
||||||
|
JsonConvert.SerializeObject(new KeyValuePair<int, string>(123, "test value"), Formatting.Indented, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""key"": 123,
|
||||||
|
""value"": ""test value""
|
||||||
|
}", json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
@@ -35,6 +36,9 @@ namespace Newtonsoft.Json.Converters
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class KeyValuePairConverter : JsonConverter
|
public class KeyValuePairConverter : JsonConverter
|
||||||
{
|
{
|
||||||
|
private const string KeyName = "Key";
|
||||||
|
private const string ValueName = "Value";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes the JSON representation of the object.
|
/// Writes the JSON representation of the object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -44,13 +48,16 @@ namespace Newtonsoft.Json.Converters
|
|||||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||||
{
|
{
|
||||||
Type t = value.GetType();
|
Type t = value.GetType();
|
||||||
PropertyInfo keyProperty = t.GetProperty("Key");
|
PropertyInfo keyProperty = t.GetProperty(KeyName);
|
||||||
PropertyInfo valueProperty = t.GetProperty("Value");
|
PropertyInfo valueProperty = t.GetProperty(ValueName);
|
||||||
|
|
||||||
|
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
|
||||||
|
|
||||||
writer.WriteStartObject();
|
writer.WriteStartObject();
|
||||||
writer.WritePropertyName("Key");
|
|
||||||
|
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName);
|
||||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
|
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
|
||||||
writer.WritePropertyName("Value");
|
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName);
|
||||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
|
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
|
||||||
writer.WriteEndObject();
|
writer.WriteEndObject();
|
||||||
}
|
}
|
||||||
@@ -90,19 +97,20 @@ namespace Newtonsoft.Json.Converters
|
|||||||
|
|
||||||
while (reader.TokenType == JsonToken.PropertyName)
|
while (reader.TokenType == JsonToken.PropertyName)
|
||||||
{
|
{
|
||||||
switch (reader.Value.ToString())
|
string propertyName = reader.Value.ToString();
|
||||||
|
if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
case "Key":
|
reader.Read();
|
||||||
reader.Read();
|
key = serializer.Deserialize(reader, keyType);
|
||||||
key = serializer.Deserialize(reader, keyType);
|
}
|
||||||
break;
|
else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase))
|
||||||
case "Value":
|
{
|
||||||
reader.Read();
|
reader.Read();
|
||||||
value = serializer.Deserialize(reader, valueType);
|
value = serializer.Deserialize(reader, valueType);
|
||||||
break;
|
}
|
||||||
default:
|
else
|
||||||
reader.Skip();
|
{
|
||||||
break;
|
reader.Skip();
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.Read();
|
reader.Read();
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.6.15009")]
|
[assembly: AssemblyFileVersion("4.5.7.15030")]
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
Reference in New Issue
Block a user