Merge branch 'issue-143-dynamic-property-types' of https://github.com/Athari/Newtonsoft.Json into Athari-issue-143-dynamic-property-types

This commit is contained in:
James Newton-King
2013-12-08 11:35:13 +13:00
4 changed files with 14 additions and 47 deletions
@@ -37,8 +37,8 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
{ {
JObject o = JObject.Parse("{prop1:'12345!',prop2:[1,'two','III']}"); JObject o = JObject.Parse("{prop1:'12345!',prop2:[1,'two','III']}");
JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1", typeof(string)); JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1");
JPropertyDescriptor prop2 = new JPropertyDescriptor("prop2", typeof(JArray)); JPropertyDescriptor prop2 = new JPropertyDescriptor("prop2");
Assert.AreEqual("12345!", ((JValue)prop1.GetValue(o)).Value); Assert.AreEqual("12345!", ((JValue)prop1.GetValue(o)).Value);
Assert.AreEqual(o["prop2"], prop2.GetValue(o)); Assert.AreEqual(o["prop2"], prop2.GetValue(o));
@@ -49,7 +49,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
{ {
JObject o = JObject.Parse("{prop1:'12345!'}"); JObject o = JObject.Parse("{prop1:'12345!'}");
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1");
propertyDescriptor1.SetValue(o, "54321!"); propertyDescriptor1.SetValue(o, "54321!");
@@ -61,7 +61,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
{ {
JObject o = JObject.Parse("{prop1:'12345!'}"); JObject o = JObject.Parse("{prop1:'12345!'}");
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1");
propertyDescriptor1.ResetValue(o); propertyDescriptor1.ResetValue(o);
Assert.AreEqual("12345!", (string)o["prop1"]); Assert.AreEqual("12345!", (string)o["prop1"]);
@@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
[Test] [Test]
public void IsReadOnly() public void IsReadOnly()
{ {
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string)); JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1");
Assert.AreEqual(false, propertyDescriptor1.IsReadOnly); Assert.AreEqual(false, propertyDescriptor1.IsReadOnly);
} }
@@ -78,9 +78,9 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
[Test] [Test]
public void PropertyType() 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);
} }
} }
} }
+4 -18
View File
@@ -1694,40 +1694,26 @@ Parameter name: arrayIndex",
[Test] [Test]
public void GetProperties() 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; ICustomTypeDescriptor descriptor = o;
PropertyDescriptorCollection properties = descriptor.GetProperties(); PropertyDescriptorCollection properties = descriptor.GetProperties();
Assert.AreEqual(4, properties.Count); Assert.AreEqual(2, properties.Count);
PropertyDescriptor prop1 = properties[0]; PropertyDescriptor prop1 = properties[0];
Assert.AreEqual("prop1", prop1.Name); 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(typeof(JObject), prop1.ComponentType);
Assert.AreEqual(false, prop1.CanResetValue(o)); Assert.AreEqual(false, prop1.CanResetValue(o));
Assert.AreEqual(false, prop1.ShouldSerializeValue(o)); Assert.AreEqual(false, prop1.ShouldSerializeValue(o));
PropertyDescriptor prop2 = properties[1]; PropertyDescriptor prop2 = properties[1];
Assert.AreEqual("prop2", prop2.Name); 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(typeof(JObject), prop2.ComponentType);
Assert.AreEqual(false, prop2.CanResetValue(o)); Assert.AreEqual(false, prop2.CanResetValue(o));
Assert.AreEqual(false, prop2.ShouldSerializeValue(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 #endif
+1 -12
View File
@@ -611,17 +611,6 @@ namespace Newtonsoft.Json.Linq
return ((ICustomTypeDescriptor)this).GetProperties(null); 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();
}
/// <summary> /// <summary>
/// Returns the properties for this instance of a component using the attribute array as a filter. /// Returns the properties for this instance of a component using the attribute array as a filter.
/// </summary> /// </summary>
@@ -635,7 +624,7 @@ namespace Newtonsoft.Json.Linq
foreach (KeyValuePair<string, JToken> propertyValue in this) foreach (KeyValuePair<string, JToken> propertyValue in this)
{ {
descriptors.Add(new JPropertyDescriptor(propertyValue.Key, GetTokenPropertyType(propertyValue.Value))); descriptors.Add(new JPropertyDescriptor(propertyValue.Key));
} }
return descriptors; return descriptors;
@@ -26,7 +26,6 @@
#if !(NETFX_CORE || PORTABLE || PORTABLE40) #if !(NETFX_CORE || PORTABLE || PORTABLE40)
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Linq namespace Newtonsoft.Json.Linq
{ {
@@ -35,20 +34,13 @@ namespace Newtonsoft.Json.Linq
/// </summary> /// </summary>
public class JPropertyDescriptor : PropertyDescriptor public class JPropertyDescriptor : PropertyDescriptor
{ {
private readonly Type _propertyType;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JPropertyDescriptor"/> class. /// Initializes a new instance of the <see cref="JPropertyDescriptor"/> class.
/// </summary> /// </summary>
/// <param name="name">The name.</param> /// <param name="name">The name.</param>
/// <param name="propertyType">Type of the property.</param> public JPropertyDescriptor(string name)
public JPropertyDescriptor(string name, Type propertyType)
: base(name, null) : base(name, null)
{ {
ValidationUtils.ArgumentNotNull(name, "name");
ValidationUtils.ArgumentNotNull(propertyType, "propertyType");
_propertyType = propertyType;
} }
private static JObject CastInstance(object instance) private static JObject CastInstance(object instance)
@@ -149,7 +141,7 @@ namespace Newtonsoft.Json.Linq
/// </returns> /// </returns>
public override Type PropertyType public override Type PropertyType
{ {
get { return _propertyType; } get { return typeof(object); }
} }
/// <summary> /// <summary>