-Changed DataContractAttribute on collections back to array serialization

-Fixed JObject.GetPropertyOwner to return value
-Fixed JPropertyDescriptor GetValue and SetValue to return null for null owner
This commit is contained in:
James Newton-King
2018-03-12 15:43:28 +13:00
parent 4f1aa395db
commit c72f785b9c
6 changed files with 57 additions and 21 deletions
@@ -60,13 +60,13 @@ namespace Newtonsoft.Json.Tests.Issues
}; };
string json = JsonConvert.SerializeObject(activities, Formatting.Indented); string json = JsonConvert.SerializeObject(activities, Formatting.Indented);
StringAssert.AreEqual(@"{ // note that this has been reverted back in 11.0.2 because it is causing compat issues
""List"": [ // https://github.com/JamesNK/Newtonsoft.Json/issues/1627
{ StringAssert.AreEqual(@"[
""Name"": ""An activity"" {
} ""Name"": ""An activity""
] }
}", json); ]", json);
} }
[Test] [Test]
@@ -50,6 +50,14 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
Assert.AreEqual(o["prop2"], prop2.GetValue(o)); Assert.AreEqual(o["prop2"], prop2.GetValue(o));
} }
[Test]
public void GetValue_NullOwner_ReturnsNull()
{
JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1");
Assert.AreEqual(null, prop1.GetValue(null));
}
[Test] [Test]
public void SetValue() public void SetValue()
{ {
@@ -62,6 +70,14 @@ namespace Newtonsoft.Json.Tests.Linq.ComponentModel
Assert.AreEqual("54321!", (string)o["prop1"]); Assert.AreEqual("54321!", (string)o["prop1"]);
} }
[Test]
public void SetValue_NullOwner_NoError()
{
JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1");
prop1.SetValue(null, "value!");
}
[Test] [Test]
public void ResetValue() public void ResetValue()
{ {
@@ -2040,5 +2040,28 @@ Parameter name: arrayIndex");
ExceptionAssert.Throws<JsonReaderException>(() => JObject.Parse(json), ExceptionAssert.Throws<JsonReaderException>(() => JObject.Parse(json),
"Additional text encountered after finished reading JSON content: [. Path '', line 3, position 0."); "Additional text encountered after finished reading JSON content: [. Path '', line 3, position 0.");
} }
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD2_0
[Test]
public void GetPropertyOwner_ReturnsJObject()
{
ICustomTypeDescriptor o = new JObject
{
["prop1"] = 1
};
PropertyDescriptorCollection properties = o.GetProperties();
Assert.AreEqual(1, properties.Count);
PropertyDescriptor pd = properties[0];
Assert.AreEqual("prop1", pd.Name);
object owner = o.GetPropertyOwner(pd);
Assert.AreEqual(o, owner);
object value = pd.GetValue(owner);
Assert.AreEqual(1, (int)(JToken)value);
}
#endif
} }
} }
+5
View File
@@ -780,6 +780,11 @@ namespace Newtonsoft.Json.Linq
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{ {
if (pd is JPropertyDescriptor)
{
return this;
}
return null; return null;
} }
#endregion #endregion
@@ -69,9 +69,7 @@ namespace Newtonsoft.Json.Linq
/// <param name="component">The component with the property for which to retrieve the value.</param> /// <param name="component">The component with the property for which to retrieve the value.</param>
public override object GetValue(object component) public override object GetValue(object component)
{ {
JToken token = CastInstance(component)[Name]; return (component as JObject)?[Name];
return token;
} }
/// <summary> /// <summary>
@@ -89,9 +87,12 @@ namespace Newtonsoft.Json.Linq
/// <param name="value">The new value.</param> /// <param name="value">The new value.</param>
public override void SetValue(object component, object value) public override void SetValue(object component, object value)
{ {
JToken token = value as JToken ?? new JValue(value); if (component is JObject o)
{
JToken token = value as JToken ?? new JValue(value);
CastInstance(component)[Name] = token; o[Name] = token;
}
} }
/// <summary> /// <summary>
@@ -1176,15 +1176,6 @@ namespace Newtonsoft.Json.Serialization
return CreateDictionaryContract(objectType); return CreateDictionaryContract(objectType);
} }
#if HAVE_DATA_CONTRACTS
// don't use GetDataContractAttribute because it looks for the attribute on base classes
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetCachedAttribute<DataContractAttribute>(objectType);
if (dataContractAttribute != null)
{
return CreateObjectContract(objectType);
}
#endif
if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken))) if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
{ {
return CreateLinqContract(objectType); return CreateLinqContract(objectType);