-Tests
-Fixed regression
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
@@ -67,7 +68,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
// ]
|
||||
//}
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); }, @"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 10.");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
}, @"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 10.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -153,5 +157,67 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
MissingMemberHandling = MissingMemberHandling.Error
|
||||
});
|
||||
}
|
||||
|
||||
public class Name
|
||||
{
|
||||
public string First { get; set; }
|
||||
}
|
||||
|
||||
public class Person
|
||||
{
|
||||
public Name Name { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingMemberHandling_RootObject()
|
||||
{
|
||||
IList<string> errors = new List<string>();
|
||||
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings
|
||||
{
|
||||
//This works on properties but not on a objects property.
|
||||
/* So nameERROR:{"first":"ni"} would throw. The payload name:{"firstERROR":"hi"} would not */
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
Error = (sender, args) =>
|
||||
{
|
||||
// A more concrete error type would be nice but we are limited by Newtonsofts library here.
|
||||
errors.Add(args.ErrorContext.Error.Message);
|
||||
args.ErrorContext.Handled = true;
|
||||
}
|
||||
};
|
||||
|
||||
Person p = new Person();
|
||||
|
||||
JsonConvert.PopulateObject(@"{nameERROR:{""first"":""hi""}}", p, settings);
|
||||
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Could not find member 'nameERROR' on object of type 'Person'. Path 'nameERROR', line 1, position 11.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingMemberHandling_InnerObject()
|
||||
{
|
||||
IList<string> errors = new List<string>();
|
||||
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings
|
||||
{
|
||||
//This works on properties but not on a objects property.
|
||||
/* So nameERROR:{"first":"ni"} would throw. The payload name:{"firstERROR":"hi"} would not */
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
Error = (sender, args) =>
|
||||
{
|
||||
// A more concrete error type would be nice but we are limited by Newtonsofts library here.
|
||||
errors.Add(args.ErrorContext.Error.Message);
|
||||
args.ErrorContext.Handled = true;
|
||||
}
|
||||
};
|
||||
|
||||
Person p = new Person();
|
||||
|
||||
JsonConvert.PopulateObject(@"{name:{""firstERROR"":""hi""}}", p, settings);
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -921,7 +921,15 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
IList<string> errorMessages = new List<string>();
|
||||
|
||||
JsonReader reader = new JsonTextReader(new StringReader("{\"string1\":\"blah\",\"int1\":2147483648,\"string2\":\"also blah\",\"int2\":2147483648,\"string3\":\"more blah\"}"));
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"{
|
||||
""string1"": ""blah"",
|
||||
""int1"": 2147483648,
|
||||
""string2"": ""also blah"",
|
||||
""int2"": 2147483648,
|
||||
""string3"": ""more blah"",
|
||||
""dateTime1"": ""200NOTDATE"",
|
||||
""string4"": ""even more blah""
|
||||
}"));
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings();
|
||||
settings.Error = (sender, args) =>
|
||||
{
|
||||
@@ -938,10 +946,13 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual("also blah", data.String2);
|
||||
Assert.AreEqual(0, data.Int2);
|
||||
Assert.AreEqual("more blah", data.String3);
|
||||
Assert.AreEqual(default(DateTime), data.DateTime1);
|
||||
Assert.AreEqual("even more blah", data.String4);
|
||||
|
||||
Assert.AreEqual(2, errorMessages.Count);
|
||||
Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int1', line 1, position 35.", errorMessages[0]);
|
||||
Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int2', line 1, position 75.", errorMessages[1]);
|
||||
//Assert.AreEqual(2, errorMessages.Count);
|
||||
Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int1', line 3, position 20.", errorMessages[0]);
|
||||
Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int2', line 5, position 20.", errorMessages[1]);
|
||||
Assert.AreEqual("Could not convert string to DateTime: 200NOTDATE. Path 'dateTime1', line 7, position 27.", errorMessages[2]);
|
||||
}
|
||||
|
||||
private class DataModel
|
||||
@@ -951,6 +962,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
public string String2 { get; set; }
|
||||
public int Int2 { get; set; }
|
||||
public string String3 { get; set; }
|
||||
public DateTime DateTime1 { get; set; }
|
||||
public string String4 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user