From 7d874c649bdeb9d841c6f84be61cee0c90b7db70 Mon Sep 17 00:00:00 2001 From: Athari Date: Wed, 23 Oct 2013 17:15:11 +0400 Subject: [PATCH] Fixed #143 (JObject's implementation of ICustomTypeDescriptor.GetProperties returns incorrect property types) --- .../JPropertyDescriptorTests.cs | 14 ++++++------ .../Linq/JObjectTests.cs | 22 ++++--------------- Src/Newtonsoft.Json/Linq/JObject.cs | 13 +---------- .../Linq/JPropertyDescriptor.cs | 12 ++-------- 4 files changed, 14 insertions(+), 47 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs index fc8dc557..2cf7a5e2 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/ComponentModel/JPropertyDescriptorTests.cs @@ -37,8 +37,8 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel { JObject o = JObject.Parse("{prop1:'12345!',prop2:[1,'two','III']}"); - JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1", typeof(string)); - JPropertyDescriptor prop2 = new JPropertyDescriptor("prop2", typeof(JArray)); + JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1"); + JPropertyDescriptor prop2 = new JPropertyDescriptor("prop2"); Assert.AreEqual("12345!", ((JValue)prop1.GetValue(o)).Value); Assert.AreEqual(o["prop2"], prop2.GetValue(o)); @@ -49,7 +49,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel { JObject o = JObject.Parse("{prop1:'12345!'}"); - JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); + JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1"); propertyDescriptor1.SetValue(o, "54321!"); @@ -61,7 +61,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel { JObject o = JObject.Parse("{prop1:'12345!'}"); - JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); + JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1"); propertyDescriptor1.ResetValue(o); Assert.AreEqual("12345!", (string)o["prop1"]); @@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel [Test] public void IsReadOnly() { - JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); + JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1"); Assert.AreEqual(false, propertyDescriptor1.IsReadOnly); } @@ -78,9 +78,9 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel [Test] public void PropertyType() { - JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); + JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1"); - Assert.AreEqual(typeof(string), propertyDescriptor1.PropertyType); + Assert.AreEqual(typeof(object), propertyDescriptor1.PropertyType); } } } diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index a0c65860..4a54f5cb 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -1694,40 +1694,26 @@ Parameter name: arrayIndex", [Test] public void GetProperties() { - JObject o = JObject.Parse("{'prop1':12,'prop2':'hi!','prop3':null,'prop4':[1,2,3]}"); + JObject o = JObject.Parse("{'prop1':12,'prop2':'hi!'}"); ICustomTypeDescriptor descriptor = o; PropertyDescriptorCollection properties = descriptor.GetProperties(); - Assert.AreEqual(4, properties.Count); + Assert.AreEqual(2, properties.Count); PropertyDescriptor prop1 = properties[0]; Assert.AreEqual("prop1", prop1.Name); - Assert.AreEqual(typeof(long), prop1.PropertyType); + Assert.AreEqual(typeof(object), prop1.PropertyType); Assert.AreEqual(typeof(JObject), prop1.ComponentType); Assert.AreEqual(false, prop1.CanResetValue(o)); Assert.AreEqual(false, prop1.ShouldSerializeValue(o)); PropertyDescriptor prop2 = properties[1]; Assert.AreEqual("prop2", prop2.Name); - Assert.AreEqual(typeof(string), prop2.PropertyType); + Assert.AreEqual(typeof(object), prop2.PropertyType); Assert.AreEqual(typeof(JObject), prop2.ComponentType); Assert.AreEqual(false, prop2.CanResetValue(o)); Assert.AreEqual(false, prop2.ShouldSerializeValue(o)); - - PropertyDescriptor prop3 = properties[2]; - Assert.AreEqual("prop3", prop3.Name); - Assert.AreEqual(typeof(object), prop3.PropertyType); - Assert.AreEqual(typeof(JObject), prop3.ComponentType); - Assert.AreEqual(false, prop3.CanResetValue(o)); - Assert.AreEqual(false, prop3.ShouldSerializeValue(o)); - - PropertyDescriptor prop4 = properties[3]; - Assert.AreEqual("prop4", prop4.Name); - Assert.AreEqual(typeof(JArray), prop4.PropertyType); - Assert.AreEqual(typeof(JObject), prop4.ComponentType); - Assert.AreEqual(false, prop4.CanResetValue(o)); - Assert.AreEqual(false, prop4.ShouldSerializeValue(o)); } #endif diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index bdf44679..379954c3 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -611,17 +611,6 @@ namespace Newtonsoft.Json.Linq return ((ICustomTypeDescriptor)this).GetProperties(null); } - private static Type GetTokenPropertyType(JToken token) - { - if (token is JValue) - { - JValue v = (JValue)token; - return (v.Value != null) ? v.Value.GetType() : typeof(object); - } - - return token.GetType(); - } - /// /// Returns the properties for this instance of a component using the attribute array as a filter. /// @@ -635,7 +624,7 @@ namespace Newtonsoft.Json.Linq foreach (KeyValuePair propertyValue in this) { - descriptors.Add(new JPropertyDescriptor(propertyValue.Key, GetTokenPropertyType(propertyValue.Value))); + descriptors.Add(new JPropertyDescriptor(propertyValue.Key)); } return descriptors; diff --git a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs index 1570d50a..71360a83 100644 --- a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs +++ b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs @@ -26,7 +26,6 @@ #if !(NETFX_CORE || PORTABLE || PORTABLE40) using System; using System.ComponentModel; -using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Linq { @@ -35,20 +34,13 @@ namespace Newtonsoft.Json.Linq /// public class JPropertyDescriptor : PropertyDescriptor { - private readonly Type _propertyType; - /// /// Initializes a new instance of the class. /// /// The name. - /// Type of the property. - public JPropertyDescriptor(string name, Type propertyType) + public JPropertyDescriptor(string name) : base(name, null) { - ValidationUtils.ArgumentNotNull(name, "name"); - ValidationUtils.ArgumentNotNull(propertyType, "propertyType"); - - _propertyType = propertyType; } private static JObject CastInstance(object instance) @@ -149,7 +141,7 @@ namespace Newtonsoft.Json.Linq /// public override Type PropertyType { - get { return _propertyType; } + get { return typeof(object); } } ///