-Refactor ExceptionAssert
This commit is contained in:
@@ -67,14 +67,12 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void FailWithPrivateConstructorPlusParametizedAndDefault()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
null,
|
||||
() =>
|
||||
{
|
||||
string json = @"{Name:""Name!""}";
|
||||
ExceptionAssert.Throws<Exception>(() =>
|
||||
{
|
||||
string json = @"{Name:""Name!""}";
|
||||
|
||||
PrivateConstructorWithPublicParametizedConstructorTestClass c = JsonConvert.DeserializeObject<PrivateConstructorWithPublicParametizedConstructorTestClass>(json);
|
||||
});
|
||||
PrivateConstructorWithPublicParametizedConstructorTestClass c = JsonConvert.DeserializeObject<PrivateConstructorWithPublicParametizedConstructorTestClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -161,12 +161,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.IsNull(contract.DefaultCreator);
|
||||
Assert.IsNull(contract.OverrideCreator);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractTestClass. Type is an interface or abstract class and cannot be instantiated. Path 'Value', line 1, position 7.",
|
||||
() => JsonConvert.DeserializeObject<AbstractTestClass>(@"{Value:'Value!'}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<AbstractTestClass>(@"{Value:'Value!'}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}), "Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractTestClass. Type is an interface or abstract class and cannot be instantiated. Path 'Value', line 1, position 7.");
|
||||
|
||||
contract.DefaultCreator = () => new AbstractImplementationTestClass();
|
||||
|
||||
@@ -188,12 +186,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.IsNull(contract.DefaultCreator);
|
||||
Assert.IsFalse(contract.HasParametrizedCreator);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractListTestClass`1[System.Int32]. Type is an interface or abstract class and cannot be instantiated. Path '', line 1, position 1.",
|
||||
() => JsonConvert.DeserializeObject<AbstractListTestClass<int>>(@"[1,2]", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<AbstractListTestClass<int>>(@"[1,2]", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}), "Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractListTestClass`1[System.Int32]. Type is an interface or abstract class and cannot be instantiated. Path '', line 1, position 1.");
|
||||
|
||||
contract.DefaultCreator = () => new AbstractImplementationListTestClass<int>();
|
||||
|
||||
@@ -270,12 +266,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.IsNull(contract.DefaultCreator);
|
||||
Assert.IsFalse(contract.HasParametrizedCreator);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractDictionaryTestClass`2[System.String,System.Int32]. Type is an interface or abstract class and cannot be instantiated. Path 'key1', line 1, position 6.",
|
||||
() => JsonConvert.DeserializeObject<AbstractDictionaryTestClass<string, int>>(@"{key1:1,key2:2}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<AbstractDictionaryTestClass<string, int>>(@"{key1:1,key2:2}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
}), "Could not create an instance of type Newtonsoft.Json.Tests.Serialization.AbstractDictionaryTestClass`2[System.String,System.Int32]. Type is an interface or abstract class and cannot be instantiated. Path 'key1', line 1, position 6.");
|
||||
|
||||
contract.DefaultCreator = () => new AbstractImplementationDictionaryTestClass<string, int>();
|
||||
|
||||
|
||||
@@ -155,17 +155,16 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void NoPublicDefaultConstructor()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unable to find a default constructor to use for type System.Dynamic.DynamicObject. Path 'contributors', line 2, position 18.",
|
||||
() =>
|
||||
{
|
||||
var settings = new JsonSerializerSettings();
|
||||
settings.NullValueHandling = NullValueHandling.Ignore;
|
||||
var json = @"{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
var settings = new JsonSerializerSettings();
|
||||
settings.NullValueHandling = NullValueHandling.Ignore;
|
||||
var json = @"{
|
||||
""contributors"": null
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<DynamicObject>(json, settings);
|
||||
});
|
||||
JsonConvert.DeserializeObject<DynamicObject>(json, settings);
|
||||
}, "Unable to find a default constructor to use for type System.Dynamic.DynamicObject. Path 'contributors', line 2, position 18.");
|
||||
}
|
||||
|
||||
public class DictionaryDynamicObject : DynamicObject
|
||||
|
||||
@@ -119,9 +119,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""3""
|
||||
]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot create and populate list type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+EnumerableClassFailure`1[System.String]. Path '', line 1, position 1.",
|
||||
() => JsonConvert.DeserializeObject<EnumerableClassFailure<string>>(json));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<EnumerableClassFailure<string>>(json), "Cannot create and populate list type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+EnumerableClassFailure`1[System.String]. Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
public class PrivateDefaultCtorList<T> : List<T>
|
||||
@@ -134,10 +132,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void DeserializePrivateListCtor()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+PrivateDefaultCtorList`1[System.Int32]. Path '', line 1, position 1.",
|
||||
() => JsonConvert.DeserializeObject<PrivateDefaultCtorList<int>>("[1,2]")
|
||||
);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<PrivateDefaultCtorList<int>>("[1,2]"), "Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+PrivateDefaultCtorList`1[System.Int32]. Path '', line 1, position 1.");
|
||||
|
||||
var list = JsonConvert.DeserializeObject<PrivateDefaultCtorList<int>>("[1,2]", new JsonSerializerSettings
|
||||
{
|
||||
@@ -621,9 +616,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"[ null ]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot convert null value to KeyValuePair. Path '[0]', line 1, position 6.",
|
||||
() => { JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json); }, "Cannot convert null value to KeyValuePair. Path '[0]', line 1, position 6.");
|
||||
}
|
||||
|
||||
#if !(NET40 || NET35 || NET20 || PORTABLE40)
|
||||
@@ -939,9 +932,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2,3],[2,1],[2,2]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array2D>(json));
|
||||
ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array2D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -949,9 +940,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1],[2,1],[2,2]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array2D>(json));
|
||||
ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array2D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -979,9 +968,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array3D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -989,9 +976,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],{}]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unexpected token when deserializing multidimensional array: StartObject. Path 'Coordinates[3][1]', line 1, position 99.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Array3D>(json), "Unexpected token when deserializing multidimensional array: StartObject. Path 'Coordinates[3][1]', line 1, position 99.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -999,9 +984,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array3D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1040,9 +1023,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[/*hi*/[/*hi*/[1/*hi*/,/*hi*/1/*hi*/,1]/*hi*/,/*hi*/[1,1";
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
null,
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
ExceptionAssert.Throws<JsonException>(() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1050,9 +1031,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[/*hi*/[/*hi*/";
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
null,
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
ExceptionAssert.Throws<JsonException>(() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -1383,9 +1383,7 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void BadJsonPropertyClassSerialize()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"A member with the name 'pie' already exists on 'Newtonsoft.Json.Tests.TestObjects.BadJsonPropertyClass'. Use the JsonPropertyAttribute to specify another name.",
|
||||
() => { JsonConvert.SerializeObject(new BadJsonPropertyClass()); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.SerializeObject(new BadJsonPropertyClass()); }, @"A member with the name 'pie' already exists on 'Newtonsoft.Json.Tests.TestObjects.BadJsonPropertyClass'. Use the JsonPropertyAttribute to specify another name.");
|
||||
}
|
||||
#if !NET20
|
||||
[Test]
|
||||
@@ -1429,9 +1427,7 @@ keyword such as type of business.""
|
||||
{
|
||||
string json = @"[""vvv\jvvv""]";
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
@"Bad JSON escape sequence: \j. Path '', line 1, position 7.",
|
||||
() => { JsonConvert.DeserializeObject<List<string>>(json); });
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => { JsonConvert.DeserializeObject<List<string>>(json); }, @"Bad JSON escape sequence: \j. Path '', line 1, position 7.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1938,13 +1934,11 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void IncompatibleJsonAttributeShouldThrow()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unexpected value when converting date. Expected DateTime or DateTimeOffset, got Newtonsoft.Json.Tests.TestObjects.IncompatibleJsonAttributeClass.",
|
||||
() =>
|
||||
{
|
||||
IncompatibleJsonAttributeClass c = new IncompatibleJsonAttributeClass();
|
||||
JsonConvert.SerializeObject(c);
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
IncompatibleJsonAttributeClass c = new IncompatibleJsonAttributeClass();
|
||||
JsonConvert.SerializeObject(c);
|
||||
}, "Unexpected value when converting date. Expected DateTime or DateTimeOffset, got Newtonsoft.Json.Tests.TestObjects.IncompatibleJsonAttributeClass.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2122,21 +2116,19 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void SerializeRequiredMembersClassNullRequiredValueProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot write a null value for property 'FirstName'. Property requires a value. Path ''.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
RequiredMembersClass requiredMembersClass = new RequiredMembersClass
|
||||
{
|
||||
RequiredMembersClass requiredMembersClass = new RequiredMembersClass
|
||||
{
|
||||
FirstName = null,
|
||||
BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
|
||||
LastName = null,
|
||||
MiddleName = null
|
||||
};
|
||||
FirstName = null,
|
||||
BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
|
||||
LastName = null,
|
||||
MiddleName = null
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(requiredMembersClass);
|
||||
Console.WriteLine(json);
|
||||
});
|
||||
string json = JsonConvert.SerializeObject(requiredMembersClass);
|
||||
Console.WriteLine(json);
|
||||
}, "Cannot write a null value for property 'FirstName'. Property requires a value. Path ''.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2239,9 +2231,7 @@ keyword such as type of business.""
|
||||
testClass.co = new Co();
|
||||
String strFromTest = JsonConvert.SerializeObject(testClass);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantiated. Path 'co.Name', line 1, position 14.",
|
||||
() => { InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass)JsonConvert.DeserializeObject(strFromTest, typeof(InterfacePropertyTestClass)); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass)JsonConvert.DeserializeObject(strFromTest, typeof(InterfacePropertyTestClass)); }, @"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantiated. Path 'co.Name', line 1, position 14.");
|
||||
}
|
||||
|
||||
private Person GetPerson()
|
||||
@@ -2492,15 +2482,13 @@ keyword such as type of business.""
|
||||
{
|
||||
string classRef = typeof(JsonPropertyWithHandlingValues).FullName;
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected for property 'ReferenceLoopHandlingErrorProperty' with type '" + classRef + "'. Path ''.",
|
||||
() =>
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
o.ReferenceLoopHandlingErrorProperty = o;
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
o.ReferenceLoopHandlingErrorProperty = o;
|
||||
|
||||
JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
});
|
||||
JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
}, "Self referencing loop detected for property 'ReferenceLoopHandlingErrorProperty' with type '" + classRef + "'. Path ''.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2869,9 +2857,7 @@ keyword such as type of business.""
|
||||
{
|
||||
string json = @"{""sublocation"":""AlertEmailSender.Program.Main"",""userId"":0,""type"":0,""summary"":""Loading settings variables"",""details"":null,""stackTrace"":"" at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)\r\n at System.Environment.get_StackTrace()\r\n at mr.Logging.Event..ctor(String summary) in C:\\Projects\\MRUtils\\Logging\\Event.vb:line 71\r\n at AlertEmailSender.Program.Main(String[] args) in C:\\Projects\\AlertEmailSender\\AlertEmailSender\\Program.cs:line 25"",""tag"":null,""time"":""\/Date(1249591032026-0400)\/""}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'sublocation', line 1, position 15.",
|
||||
() => { JsonConvert.DeserializeObject<TestObjects.Event>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<TestObjects.Event>(json); }, @"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'sublocation', line 1, position 15.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2910,11 +2896,9 @@ keyword such as type of business.""
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<Person>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
|
||||
Path '', line 1, position 1.",
|
||||
() => { JsonConvert.DeserializeObject<Person>(json); });
|
||||
Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2922,11 +2906,9 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<Dictionary<string, string>>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
|
||||
Path '', line 1, position 1.",
|
||||
() => { JsonConvert.DeserializeObject<Dictionary<string, string>>(json); });
|
||||
Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
@@ -2935,11 +2917,9 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Exception' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<Exception>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Exception' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
|
||||
Path '', line 1, position 1.",
|
||||
() => { JsonConvert.DeserializeObject<Exception>(json); });
|
||||
Path '', line 1, position 1.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2948,11 +2928,9 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<double>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
|
||||
Path '', line 1, position 1.",
|
||||
() => { JsonConvert.DeserializeObject<double>(json); });
|
||||
Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
#if !(NET35 || NET20 || PORTABLE40)
|
||||
@@ -2961,11 +2939,9 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.Linq.DynamicDictionary' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<DynamicDictionary>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Newtonsoft.Json.Tests.Linq.DynamicDictionary' because the type requires a JSON object (e.g. {""name"":""value""}) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON object (e.g. {""name"":""value""}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
|
||||
Path '', line 1, position 1.",
|
||||
() => { JsonConvert.DeserializeObject<DynamicDictionary>(json); });
|
||||
Path '', line 1, position 1.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2987,9 +2963,7 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"new Constructor(123)";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value ""Constructor"" to type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '', line 1, position 16.",
|
||||
() => { JsonConvert.DeserializeObject<Person>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<Person>(json); }, @"Error converting value ""Constructor"" to type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '', line 1, position 16.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2997,9 +2971,7 @@ Path '', line 1, position 1.",
|
||||
{
|
||||
string json = @"[new Constructor(123)]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value ""Constructor"" to type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '[0]', line 1, position 17.",
|
||||
() => { JsonConvert.DeserializeObject<List<Person>>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<List<Person>>(json); }, @"Error converting value ""Constructor"" to type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '[0]', line 1, position 17.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -3025,9 +2997,7 @@ Path ''"));
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot populate JSON array onto type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '', line 1, position 1.",
|
||||
() => { JsonConvert.PopulateObject(json, new Person()); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.PopulateObject(json, new Person()); }, @"Cannot populate JSON array onto type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -3035,9 +3005,7 @@ Path ''"));
|
||||
{
|
||||
string json = @"{}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot populate JSON object onto type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'. Path '', line 1, position 2.",
|
||||
() => { JsonConvert.PopulateObject(json, new List<Person>()); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.PopulateObject(json, new List<Person>()); }, @"Cannot populate JSON object onto type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'. Path '', line 1, position 2.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -3052,77 +3020,69 @@ Path ''"));
|
||||
[Test]
|
||||
public void SerializePropertyGetError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(new MemoryStream(), new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.SerializeObject(new MemoryStream(), new JsonSerializerSettings
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, @"Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializePropertySetError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error setting value to 'ReadTimeout' on 'System.IO.MemoryStream'.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:0}", new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:0}", new JsonSerializerSettings
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, @"Error setting value to 'ReadTimeout' on 'System.IO.MemoryStream'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnsureTypeEmptyStringToIntError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 15.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, @"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 15.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnsureTypeNullToIntError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 17.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, @"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 17.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -3523,12 +3483,11 @@ Path ''"));
|
||||
{
|
||||
string json = "{'$id':'1',key1:'value1',key2:'value2',key3:'value3'}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Cannot preserve reference to readonly dictionary, or dictionary created from a non-default constructor: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor. Path 'key1', line 1, position 16.",
|
||||
() => JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
}));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json, new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
}), "Cannot preserve reference to readonly dictionary, or dictionary created from a non-default constructor: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor. Path 'key1', line 1, position 16.");
|
||||
}
|
||||
|
||||
public class DictionaryWithNoDefaultConstructor : Dictionary<string, string>
|
||||
@@ -3914,16 +3873,14 @@ Path ''"));
|
||||
{
|
||||
try
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.
|
||||
Path 'booleanValue', line 1, position 14.",
|
||||
() =>
|
||||
{
|
||||
JsonTypeReflector.SetFullyTrusted(false);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonTypeReflector.SetFullyTrusted(false);
|
||||
|
||||
JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
|
||||
});
|
||||
JsonConvert.DeserializeObject<ISerializableTestObject>("{booleanValue:true}");
|
||||
}, @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.
|
||||
Path 'booleanValue', line 1, position 14.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -3936,16 +3893,14 @@ Path 'booleanValue', line 1, position 14.",
|
||||
{
|
||||
try
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path ''.",
|
||||
() =>
|
||||
{
|
||||
JsonTypeReflector.SetFullyTrusted(false);
|
||||
ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonTypeReflector.SetFullyTrusted(false);
|
||||
ISerializableTestObject value = new ISerializableTestObject("string!", 0, default(DateTimeOffset), null);
|
||||
|
||||
JsonConvert.SerializeObject(value);
|
||||
});
|
||||
JsonConvert.SerializeObject(value);
|
||||
}, @"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path ''.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -4440,32 +4395,28 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
var json = JsonConvert.SerializeObject(child, Formatting.Indented);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Additional content found in JSON reference object. A JSON reference object should only have a $ref property. Path 'Father.$id', line 6, position 11.",
|
||||
() => { JsonConvert.DeserializeObject<Dictionary<string, object>>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<Dictionary<string, object>>(json); }, "Additional content found in JSON reference object. A JSON reference object should only have a $ref property. Path 'Father.$id', line 6, position 11.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRefBadType()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON reference $ref property must have a string or null value. Path 'Father.$ref', line 5, position 14.",
|
||||
() =>
|
||||
{
|
||||
//Additional text found in JSON string after finishing deserializing object.
|
||||
//Test 1
|
||||
var reference = new Dictionary<string, object>();
|
||||
reference.Add("$ref", 1);
|
||||
reference.Add("$id", 1);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
//Additional text found in JSON string after finishing deserializing object.
|
||||
//Test 1
|
||||
var reference = new Dictionary<string, object>();
|
||||
reference.Add("$ref", 1);
|
||||
reference.Add("$id", 1);
|
||||
|
||||
var child = new Dictionary<string, object>();
|
||||
child.Add("_id", 2);
|
||||
child.Add("Name", "Isabell");
|
||||
child.Add("Father", reference);
|
||||
var child = new Dictionary<string, object>();
|
||||
child.Add("_id", 2);
|
||||
child.Add("Name", "Isabell");
|
||||
child.Add("Father", reference);
|
||||
|
||||
var json = JsonConvert.SerializeObject(child, Formatting.Indented);
|
||||
JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
||||
});
|
||||
var json = JsonConvert.SerializeObject(child, Formatting.Indented);
|
||||
JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
||||
}, "JSON reference $ref property must have a string or null value. Path 'Father.$ref', line 5, position 14.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -4674,9 +4625,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
string json = JsonConvert.SerializeObject(s1, Formatting.Indented);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot create and populate list type " + classRef + ". Path 'StringDictionaryProperty', line 2, position 32.",
|
||||
() => { JsonConvert.DeserializeObject<StringDictionaryTestClass>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<StringDictionaryTestClass>(json); }, "Cannot create and populate list type " + classRef + ". Path 'StringDictionaryProperty', line 2, position 32.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -5162,9 +5111,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void DeserializeNullDateTimeValueTest()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value {null} to type 'System.DateTime'. Path '', line 1, position 4.",
|
||||
() => { JsonConvert.DeserializeObject("null", typeof(DateTime)); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject("null", typeof(DateTime)); }, "Error converting value {null} to type 'System.DateTime'. Path '', line 1, position 4.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -6032,9 +5979,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
null
|
||||
]";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value {null} to type 'System.Int32'. Path '[3]', line 5, position 7.",
|
||||
() => { List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json); }, "Error converting value {null} to type 'System.Int32'. Path '[3]', line 5, position 7.");
|
||||
}
|
||||
|
||||
#if !(PORTABLE || NETFX_CORE)
|
||||
@@ -6073,9 +6018,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
""NullableInteger2"": null
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value 1 to type 'Newtonsoft.Json.Tests.ConvertibleInt'. Path 'Integer', line 2, position 15.",
|
||||
() => JsonConvert.DeserializeObject<ConvertableIntTestClass>(json));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<ConvertableIntTestClass>(json), "Error converting value 1 to type 'Newtonsoft.Json.Tests.ConvertibleInt'. Path 'Integer', line 2, position 15.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -6104,31 +6047,27 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void DeserializeBoolInt()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading integer. Unexpected token: Boolean. Path 'PreProperty', line 2, position 22.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
ExceptionAssert.Throws<JsonReaderException>(() =>
|
||||
{
|
||||
string json = @"{
|
||||
""PreProperty"": true,
|
||||
""PostProperty"": ""-1""
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
}, "Error reading integer. Unexpected token: Boolean. Path 'PreProperty', line 2, position 22.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeUnexpectedEndInt()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
null,
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
string json = @"{
|
||||
""PreProperty"": ";
|
||||
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -6275,25 +6214,19 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void DeserializeDoubleFromEmptyString()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.Double' is not nullable. Path '', line 0, position 0.",
|
||||
() => { JsonConvert.DeserializeObject<double>(""); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<double>(""); }, "No JSON content found and type 'System.Double' is not nullable. Path '', line 0, position 0.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnumFromEmptyString()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.StringComparison' is not nullable. Path '', line 0, position 0.",
|
||||
() => { JsonConvert.DeserializeObject<StringComparison>(""); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<StringComparison>(""); }, "No JSON content found and type 'System.StringComparison' is not nullable. Path '', line 0, position 0.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeInt32FromEmptyString()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.Int32' is not nullable. Path '', line 0, position 0.",
|
||||
() => { JsonConvert.DeserializeObject<int>(""); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<int>(""); }, "No JSON content found and type 'System.Int32' is not nullable. Path '', line 0, position 0.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -6925,9 +6858,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
settings.CheckAdditionalContent = true;
|
||||
s = JsonSerializer.Create(settings);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: {. Path '', line 1, position 7.",
|
||||
() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); });
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 1, position 7.");
|
||||
}
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
@@ -6971,21 +6902,19 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void AdditionalContentAfterFinish()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Additional text found in JSON string after finishing deserializing object.",
|
||||
() =>
|
||||
{
|
||||
string json = "[{},1]";
|
||||
ExceptionAssert.Throws<JsonException>(() =>
|
||||
{
|
||||
string json = "[{},1]";
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.CheckAdditionalContent = true;
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.CheckAdditionalContent = true;
|
||||
|
||||
var reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
var reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
|
||||
serializer.Deserialize(reader, typeof(MyType));
|
||||
});
|
||||
serializer.Deserialize(reader, typeof(MyType));
|
||||
}, "Additional text found in JSON string after finishing deserializing object.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -7179,8 +7108,8 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual(500, obj.Item1);
|
||||
#else
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+MyTuple`1[System.Int32]. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'm_Item1', line 1, position 11.",
|
||||
doStuff);
|
||||
doStuff,
|
||||
"Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+MyTuple`1[System.Int32]. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'm_Item1', line 1, position 11.");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -7196,9 +7125,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
var json = JsonConvert.SerializeObject(tuple);
|
||||
Assert.AreEqual(@"{""m_Item1"":500}", json);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+MyTuplePartial`1[System.Int32]. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'm_Item1', line 1, position 11.",
|
||||
() => JsonConvert.DeserializeObject<MyTuplePartial<int>>(json));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<MyTuplePartial<int>>(json), "Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+MyTuplePartial`1[System.Int32]. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'm_Item1', line 1, position 11.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -7434,9 +7361,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void NoConstructorReadOnlyCollectionTest()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot deserialize readonly or fixed size list: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyCollection`1[System.Int32]. Path '', line 1, position 1.",
|
||||
() => JsonConvert.DeserializeObject<NoConstructorReadOnlyCollection<int>>("[1]"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<NoConstructorReadOnlyCollection<int>>("[1]"), "Cannot deserialize readonly or fixed size list: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyCollection`1[System.Int32]. Path '', line 1, position 1.");
|
||||
}
|
||||
|
||||
#if !(NET40 || NET35 || NET20 || PORTABLE40)
|
||||
@@ -7451,9 +7376,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void NoConstructorReadOnlyDictionaryTest()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot deserialize readonly or fixed size dictionary: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyDictionary`2[System.Int32,System.Int32]. Path '1', line 1, position 5.",
|
||||
() => JsonConvert.DeserializeObject<NoConstructorReadOnlyDictionary<int, int>>("{'1':1}"));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<NoConstructorReadOnlyDictionary<int, int>>("{'1':1}"), "Cannot deserialize readonly or fixed size dictionary: Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+NoConstructorReadOnlyDictionary`2[System.Int32,System.Int32]. Path '1', line 1, position 5.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -7467,9 +7390,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
Assert.AreEqual(BigInteger.Parse("999999999999999999999999999999999999999999999999"), l[0]);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value 999999999999999999999999999999999999999999999999 to type 'System.Int64'. Path '[0]', line 1, position 49.",
|
||||
() => JsonConvert.DeserializeObject<IList<long>>(json));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<IList<long>>(json), "Error converting value 999999999999999999999999999999999999999999999999 to type 'System.Int64'. Path '[0]', line 1, position 49.");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -7977,13 +7898,11 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void DuplicatePropertiesInNestedObject()
|
||||
{
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
"Can not add property time to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
|
||||
() =>
|
||||
{
|
||||
string content = @"{""result"":{""time"":1408188592,""time"":1408188593},""error"":null,""id"":""1""}";
|
||||
JsonConvert.DeserializeObject<JObject>(content);
|
||||
});
|
||||
ExceptionAssert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
string content = @"{""result"":{""time"":1408188592,""time"":1408188593},""error"":null,""id"":""1""}";
|
||||
JsonConvert.DeserializeObject<JObject>(content);
|
||||
}, "Can not add property time to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -8247,17 +8166,13 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
[Test]
|
||||
public void ErrorCreatingJsonConverter()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Error creating 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ErroringJsonConverter'.",
|
||||
() => JsonConvert.SerializeObject(new ErroringTestClass()));
|
||||
ExceptionAssert.Throws<JsonException>(() => JsonConvert.SerializeObject(new ErroringTestClass()), "Error creating 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ErroringJsonConverter'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeInvalidOctalRootError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Input string '020474068' is not a valid number. Path '', line 1, position 9.",
|
||||
() => JsonConvert.DeserializeObject<string>("020474068"));
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<string>("020474068"), "Input string '020474068' is not a valid number. Path '', line 1, position 9.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -99,17 +99,15 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
]
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot preserve reference to array or readonly list, or list created from a non-default constructor: System.String[][]. Path '$values', line 3, position 15.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
}, @"Cannot preserve reference to array or readonly list, or list created from a non-default constructor: System.String[][]. Path '$values', line 3, position 15.");
|
||||
}
|
||||
|
||||
#if !NETFX_CORE
|
||||
@@ -230,16 +228,14 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""Manager"": null
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
}, "Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -537,23 +533,21 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
Assert.AreEqual(5d, actual.Payload);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
|
||||
Path 'Payload.$type', line 3, position 13.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<ItemWithTypedPayload>(@"{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<ItemWithTypedPayload>(@"{
|
||||
""Payload"": {
|
||||
""$type"": ""System.Double, mscorlib"",
|
||||
""$value"": ""5""
|
||||
}
|
||||
}",
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore
|
||||
});
|
||||
});
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Ignore
|
||||
});
|
||||
}, @"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
|
||||
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
|
||||
Path 'Payload.$type', line 3, position 13.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -63,9 +63,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
// ]
|
||||
//}
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 11.",
|
||||
() => { ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); });
|
||||
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 11.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -128,9 +126,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
string json = @"{""Missing"":1}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Could not find member 'Missing' on object of type 'DoubleClass'. Path 'Missing', line 1, position 11.",
|
||||
() => { JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); }, "Could not find member 'Missing' on object of type 'DoubleClass'. Path 'Missing', line 1, position 11.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -157,8 +157,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void PopulateWithBadJson()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected initial token 'Integer' when populating object. Expected JSON object or array. Path '', line 1, position 1.",
|
||||
() => { JsonConvert.PopulateObject("1", new Person()); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.PopulateObject("1", new Person()); }, "Unexpected initial token 'Integer' when populating object. Expected JSON object or array. Path '', line 1, position 1.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,9 +105,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
circularList.Add(new CircularList { null });
|
||||
circularList.Add(new CircularList { new CircularList { circularList } });
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type '" + classRef + "'. Path '[2][0]'.",
|
||||
() => { JsonConvert.SerializeObject(circularList, Formatting.Indented); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.SerializeObject(circularList, Formatting.Indented); }, "Self referencing loop detected with type '" + classRef + "'. Path '[2][0]'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -240,13 +238,11 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
]
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot preserve reference to array or readonly list, or list created from a non-default constructor: System.String[][]. Path '$values', line 3, position 15.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
}, @"Cannot preserve reference to array or readonly list, or list created from a non-default constructor: System.String[][]. Path '$values', line 3, position 15.");
|
||||
}
|
||||
|
||||
public class CircularDictionary : Dictionary<string, CircularDictionary>
|
||||
@@ -262,9 +258,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
circularDictionary.Add("other", new CircularDictionary { { "blah", null } });
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Self referencing loop detected with type '" + classRef + "'. Path ''.",
|
||||
() => { JsonConvert.SerializeObject(circularDictionary, Formatting.Indented); });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.SerializeObject(circularDictionary, Formatting.Indented); }, @"Self referencing loop detected with type '" + classRef + "'. Path ''.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -290,17 +284,15 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
string json = @"{
|
||||
""$id"":";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Unexpected end when deserializing object. Path '$id', line 2, position 9.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
});
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
});
|
||||
}, @"Unexpected end when deserializing object. Path '$id', line 2, position 9.");
|
||||
}
|
||||
|
||||
public class CircularReferenceClassConverter : JsonConverter
|
||||
@@ -1065,12 +1057,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error reading object reference '1'. Path 'Data.Prop2.MyProperty', line 9, position 20.",
|
||||
() => JsonConvert.DeserializeObject<PropertyItemIsReferenceObject>(json, new JsonSerializerSettings
|
||||
{
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
}));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<PropertyItemIsReferenceObject>(json, new JsonSerializerSettings
|
||||
{
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.Default
|
||||
}), "Error reading object reference '1'. Path 'Data.Prop2.MyProperty', line 9, position 20.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -186,9 +186,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
var settings =
|
||||
new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+MainClass'. Path 'Child'.",
|
||||
() => JsonConvert.SerializeObject(main, settings));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.SerializeObject(main, settings), "Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+MainClass'. Path 'Child'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -247,9 +245,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
var settings = new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+DictionaryDynamicObject'. Path 'child'.",
|
||||
() => JsonConvert.SerializeObject(parent, settings));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.SerializeObject(parent, settings), "Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+DictionaryDynamicObject'. Path 'child'.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -728,9 +728,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
JsonTextReader jReader = new JsonTextReader(new StreamReader(stream));
|
||||
JsonSerializer s = new JsonSerializer();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
@"Unterminated string. Expected delimiter: "". Path '', line 1, position 3.",
|
||||
() => { ErrorTestObject obj = s.Deserialize<ErrorTestObject>(jReader); });
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => { ErrorTestObject obj = s.Deserialize<ErrorTestObject>(jReader); }, @"Unterminated string. Expected delimiter: "". Path '', line 1, position 3.");
|
||||
}
|
||||
|
||||
public class RootThing
|
||||
@@ -833,9 +831,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
string foo = "{ something: { rootSomethingElse { somethingElse: 0 } } }";
|
||||
var reader = new System.IO.StringReader(foo);
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"An error occurred.",
|
||||
() => { serialiser.Deserialize(reader, typeof(Something)); });
|
||||
ExceptionAssert.Throws<Exception>(() => { serialiser.Deserialize(reader, typeof(Something)); }, "An error occurred.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -857,9 +853,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
var writer = new System.IO.StringWriter();
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"An error occurred.",
|
||||
() => { serialiser.Serialize(writer, r); });
|
||||
ExceptionAssert.Throws<Exception>(() => { serialiser.Serialize(writer, r); }, "An error occurred.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -442,9 +442,7 @@ OnSerialized_Derived_Derived", string.Join(Environment.NewLine, e.ToArray()));
|
||||
}
|
||||
};
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Serialization Callback 'Void Deserialized()' in type 'Newtonsoft.Json.Tests.Serialization.Contract' must have a single parameter of type 'System.Runtime.Serialization.StreamingContext'.",
|
||||
() => JsonConvert.SerializeObject(d, Formatting.Indented));
|
||||
ExceptionAssert.Throws<JsonException>(() => JsonConvert.SerializeObject(d, Formatting.Indented), "Serialization Callback 'Void Deserialized()' in type 'Newtonsoft.Json.Tests.Serialization.Contract' must have a single parameter of type 'System.Runtime.Serialization.StreamingContext'.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -360,17 +360,15 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
LevelFilter = TraceLevel.Info
|
||||
};
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Could not convert string to integer: hi. Path 'Integer', line 1, position 15.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<IntegerTestClass>(
|
||||
json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
});
|
||||
ExceptionAssert.Throws<Exception>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<IntegerTestClass>(
|
||||
json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
}, "Could not convert string to integer: hi. Path 'Integer', line 1, position 15.");
|
||||
|
||||
Assert.AreEqual(2, traceWriter.TraceRecords.Count);
|
||||
|
||||
@@ -391,17 +389,15 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
LevelFilter = TraceLevel.Info
|
||||
};
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<TraceTestObject>(
|
||||
json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
});
|
||||
ExceptionAssert.Throws<Exception>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<TraceTestObject>(
|
||||
json,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
}, "Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.");
|
||||
|
||||
Assert.AreEqual(3, traceWriter.TraceRecords.Count);
|
||||
|
||||
@@ -609,17 +605,15 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
LevelFilter = TraceLevel.Verbose
|
||||
};
|
||||
|
||||
ExceptionAssert.Throws<SerializationException>(
|
||||
"Member 'ClassName' was not found.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<Exception>(
|
||||
"{}",
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
});
|
||||
ExceptionAssert.Throws<SerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<Exception>(
|
||||
"{}",
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
}, "Member 'ClassName' was not found.");
|
||||
|
||||
Assert.IsTrue(traceWriter.TraceRecords[0].Message.StartsWith("Deserializing System.Exception using ISerializable constructor. Path ''"));
|
||||
Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
|
||||
|
||||
@@ -118,13 +118,13 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
sb.Append(@"{""$value"":");
|
||||
}
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected token when deserializing primitive value: StartObject. Path '$value', line 1, position 11.", () =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
var reader = new JsonTextReader(new StringReader(sb.ToString()));
|
||||
var ser = new JsonSerializer();
|
||||
ser.MetadataPropertyHandling = MetadataPropertyHandling.Default;
|
||||
ser.Deserialize<bool>(reader);
|
||||
});
|
||||
}, "Unexpected token when deserializing primitive value: StartObject. Path '$value', line 1, position 11.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -438,15 +438,13 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""Manager"": null
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.",
|
||||
() =>
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
}, "Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.");
|
||||
}
|
||||
|
||||
public interface ICorrelatedMessage
|
||||
|
||||
Reference in New Issue
Block a user