Add MissingMemberHandling to JsonObjectAttribute (#2050)
This commit is contained in:
@@ -27,6 +27,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if DNXCORE50
|
||||
using Xunit;
|
||||
@@ -154,6 +155,20 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingErrorAttribute()
|
||||
{
|
||||
string json = @"{""Missing"":1}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<NameWithMissingError>(json); }, "Could not find member 'Missing' on object of type 'NameWithMissingError'. Path 'Missing', line 1, position 11.");
|
||||
}
|
||||
|
||||
[JsonObject(MissingMemberHandling = MissingMemberHandling.Error)]
|
||||
public class NameWithMissingError
|
||||
{
|
||||
public string First { get; set; }
|
||||
}
|
||||
|
||||
public class Name
|
||||
{
|
||||
public string First { get; set; }
|
||||
@@ -215,5 +230,46 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Could not find member 'firstERROR' on object of type 'Name'. Path 'name.firstERROR', line 1, position 20.", errors[0]);
|
||||
}
|
||||
|
||||
[JsonObject(MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
public class SimpleExtendableObject
|
||||
{
|
||||
[JsonExtensionData]
|
||||
public IDictionary<string, object> Data { get; } = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public class ObjectWithExtendableChild
|
||||
{
|
||||
public SimpleExtendableObject Data;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMissingMemberHandlingForDirectObjects()
|
||||
{
|
||||
string json = @"{""extensionData1"": [1,2,3]}";
|
||||
SimpleExtendableObject e2 = JsonConvert.DeserializeObject<SimpleExtendableObject>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
JArray o1 = (JArray)e2.Data["extensionData1"];
|
||||
Assert.AreEqual(JTokenType.Array, o1.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMissingMemberHandlingForChildObjects()
|
||||
{
|
||||
string json = @"{""Data"":{""extensionData1"": [1,2,3]}}";
|
||||
ObjectWithExtendableChild e3 = JsonConvert.DeserializeObject<ObjectWithExtendableChild>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
JArray o1 = (JArray)e3.Data.Data["extensionData1"];
|
||||
Assert.AreEqual(JTokenType.Array, o1.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMissingMemberHandlingForChildObjectsWithInvalidData()
|
||||
{
|
||||
string json = @"{""InvalidData"":{""extensionData1"": [1,2,3]}}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<ObjectWithExtendableChild>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
}, "Could not find member 'InvalidData' on object of type 'ObjectWithExtendableChild'. Path 'InvalidData', line 1, position 15.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user