Permalink
Browse files

We can now deserialize a JSON object string that contains member name…

…s that match the JsonMemberAttribute.Name on properties for

the type we are deserializing too.
  • Loading branch information...
1 parent 88f337d commit f6fa42f53408940724aaa1db8bfed740758a331e @dragan dragan committed Aug 19, 2010
View
43 src/SineSignal.Ottoman.Specs/Serialization/JsonSerializerSpecs.cs
@@ -175,28 +175,27 @@ public void Should_set_all_the_complex_types_properties_from_JSON_string()
}
}
- // TODO: Uncomment and get this test passing by adding logic to read the member attribute if one exists.
-// public class When_deserializing_a_complex_type_with_a_custom_json_member_attribute : JsonSerializerConcern
-// {
-// private string input;
-// private ComplexTypeWithCustomProperty output;
-//
-// protected override void Given()
-// {
-// input = "{\"property_one\":10}";
-// }
-//
-// protected override void When()
-// {
-// output = Sut.Deserialize<ComplexTypeWithCustomProperty>(input);
-// }
-//
-// [Test]
-// public void Should_use_custom_property_name_instead_of_property_name_when_reading()
-// {
-// Assert.That(output.Property1, Is.EqualTo(10));
-// }
-// }
+ public class When_deserializing_a_complex_type_with_a_custom_json_member_attribute : JsonSerializerConcern
+ {
+ private string input;
+ private ComplexTypeWithCustomProperty output;
+
+ protected override void Given()
+ {
+ input = "{\"property_one\":10}";
+ }
+
+ protected override void When()
+ {
+ output = Sut.Deserialize<ComplexTypeWithCustomProperty>(input);
+ }
+
+ [Test]
+ public void Should_use_json_member_attribute_name_to_set_the_property_value()
+ {
+ Assert.That(output.Property1, Is.EqualTo(10));
+ }
+ }
}
public class ComplexType
View
28 src/SineSignal.Ottoman/Serialization/JsonConvert.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.ComponentModel;
using System.Globalization;
using System.Reflection;
@@ -169,14 +168,13 @@ private static void WriteValue(object obj, JsonWriter jsonWriter)
// At this point obj must be a complex type
jsonWriter.BeginObject();
- PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(type);
- foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
+ foreach (PropertyInfo propertyInfo in type.GetProperties())
{
- object propertyValue = propertyDescriptor.GetValue(obj);
+ object propertyValue = propertyInfo.GetValue(obj, null);
if (propertyValue != null)
{
- string propertyName = GetPropertyName(propertyDescriptor);
+ string propertyName = GetPropertyName(propertyInfo);
jsonWriter.WriteMember(propertyName);
WriteValue(propertyValue, jsonWriter);
}
@@ -407,7 +405,9 @@ private static void AddObjectMetadata(Type type)
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
- if (propertyInfo.Name == "Item")
+ string propertyName = GetPropertyName(propertyInfo);
+
+ if (propertyName == "Item")
{
ParameterInfo[] parameters = propertyInfo.GetIndexParameters();
@@ -424,7 +424,7 @@ private static void AddObjectMetadata(Type type)
propertyData.Info = propertyInfo;
propertyData.Type = propertyInfo.PropertyType;
- data.Properties.Add(propertyInfo.Name, propertyData);
+ data.Properties.Add(propertyName, propertyData);
}
foreach (FieldInfo fieldInfo in type.GetFields())
@@ -489,19 +489,19 @@ private static void AddTypeProperties(Type type)
}
}
- private static string GetPropertyName(PropertyDescriptor propertyDescriptor)
+ private static string GetPropertyName(PropertyInfo propertyInfo)
{
- string propertyName = propertyDescriptor.Name;
+ string propertyName = propertyInfo.Name;
- AttributeCollection attributes = propertyDescriptor.Attributes;
+ object[] jsonMemberAttributes = propertyInfo.GetCustomAttributes(typeof(JsonMemberAttribute), true);
- if (attributes.Count > 0)
+ if (jsonMemberAttributes.Length == 1)
{
- JsonMemberAttribute jsonMember = attributes[typeof(JsonMemberAttribute)] as JsonMemberAttribute;
+ JsonMemberAttribute jsonMemberAttribute = jsonMemberAttributes[0] as JsonMemberAttribute;
- if (jsonMember != null)
+ if (jsonMemberAttribute != null)
{
- propertyName = jsonMember.Name;
+ propertyName = jsonMemberAttribute.Name;
}
}

0 comments on commit f6fa42f

Please sign in to comment.