-Added Path to JsonReaderException/JsonSerializationException messages
-Added JsonException -Fixed all Exceptions thrown to be more specific
This commit is contained in:
@@ -275,28 +275,29 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void ReadAsInt32BadString()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("Could not convert string to integer: a.",
|
||||
() =>
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to integer: a. Path '[0]'.",
|
||||
() =>
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.AreEqual(false, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
|
||||
Assert.AreEqual(false, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
|
||||
|
||||
reader.ReadRootValueAsArray = true;
|
||||
reader.DateTimeKindHandling = DateTimeKind.Utc;
|
||||
reader.ReadRootValueAsArray = true;
|
||||
reader.DateTimeKindHandling = DateTimeKind.Utc;
|
||||
|
||||
Assert.AreEqual(true, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
|
||||
Assert.AreEqual(true, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
});
|
||||
reader.ReadAsInt32();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -217,7 +217,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeNullToNonNullable()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Cannot convert null value to System.DateTime.",
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.",
|
||||
() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
|
||||
@@ -100,7 +100,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void DeserializeNullToNonNullable()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Cannot convert null value to System.DateTime.",
|
||||
ExceptionAssert.Throws<Exception>("Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.",
|
||||
() =>
|
||||
{
|
||||
DateTimeTestClass c2 =
|
||||
|
||||
@@ -491,17 +491,25 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "XmlNodeConverter can only convert JSON that begins with an object.")]
|
||||
public void NoRootObject()
|
||||
{
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"XmlNodeConverter can only convert JSON that begins with an object.",
|
||||
() =>
|
||||
{
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.")]
|
||||
public void RootObjectMultipleProperties()
|
||||
{
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() =>
|
||||
{
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -613,22 +621,30 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.")]
|
||||
public void MultipleRootPropertiesXmlDocument()
|
||||
{
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
JsonConvert.DeserializeXmlNode(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeXmlNode(json);
|
||||
});
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.")]
|
||||
public void MultipleRootPropertiesXDocument()
|
||||
{
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
JsonConvert.DeserializeXNode(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeXNode(json);
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1008,7 +1024,6 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "XmlNodeConverter cannot convert JSON with an empty property name to XML.")]
|
||||
public void EmptyPropertyName()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -1090,7 +1105,12 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}";
|
||||
|
||||
DeserializeXmlNode(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"XmlNodeConverter cannot convert JSON with an empty property name to XML.",
|
||||
() =>
|
||||
{
|
||||
DeserializeXmlNode(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -303,7 +303,7 @@ now brown cow?", '"', true);
|
||||
[Test]
|
||||
public void TestInvalidStrings()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: t. Line 1, position 19.",
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: t. Path '', line 1, position 19.",
|
||||
() =>
|
||||
{
|
||||
string orig = @"this is a string ""that has quotes"" ";
|
||||
|
||||
@@ -189,11 +189,6 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional text encountered after finished reading JSON content: ,. Line 5, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void ParseAdditionalContent_Comma()
|
||||
{
|
||||
string json = @"[
|
||||
@@ -203,17 +198,18 @@ namespace Newtonsoft.Json.Tests
|
||||
],";
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
|
||||
() =>
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional text encountered after finished reading JSON content: c. Line 5, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void ParseAdditionalContent_Text()
|
||||
{
|
||||
string json = @"[
|
||||
@@ -240,7 +236,12 @@ namespace Newtonsoft.Json.Tests
|
||||
reader.Read();
|
||||
Assert.AreEqual(5, reader.LineNumber);
|
||||
|
||||
reader.Read();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: c. Path '', line 5, position 2.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -261,19 +262,20 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional text encountered after finished reading JSON content: a. Line 1, position 5."
|
||||
#endif
|
||||
)]
|
||||
public void ParseAdditionalContent_WhitespaceThenText()
|
||||
{
|
||||
string json = @"'hi' a";
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: a. Path '', line 1, position 5.",
|
||||
() =>
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -483,27 +485,28 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: reader"
|
||||
#endif
|
||||
)]
|
||||
public void NullTextReader()
|
||||
{
|
||||
new JsonTextReader(null);
|
||||
ExceptionAssert.Throws<ArgumentNullException>(
|
||||
@"Value cannot be null.
|
||||
Parameter name: reader",
|
||||
() =>
|
||||
{
|
||||
new JsonTextReader(null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 3."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEndOfString()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader("'hi"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unterminated string. Expected delimiter: '. Path '', line 1, position 3.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -664,54 +667,58 @@ Parameter name: reader"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end while parsing unicode character. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEndOfHex()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"'h\u123"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end while parsing unicode character. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 3."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEndOfControlCharacter()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"'h\"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unterminated string. Expected delimiter: '. Path '', line 1, position 3.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading bytes. Unexpected token: Boolean. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void ReadBytesWithBadCharacter()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"true"));
|
||||
reader.ReadAsBytes();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading bytes. Unexpected token: Boolean. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unterminated string. Expected delimiter: '. Line 1, position 17."
|
||||
#endif
|
||||
)]
|
||||
public void ReadBytesWithUnexpectedEnd()
|
||||
{
|
||||
string helloWorld = "Hello world!";
|
||||
byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld);
|
||||
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"'" + Convert.ToBase64String(helloWorldData)));
|
||||
reader.ReadAsBytes();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unterminated string. Expected delimiter: '. Path '', line 1, position 17.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -725,16 +732,17 @@ Parameter name: reader"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end while parsing unquoted property name. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEndWhenParsingUnquotedProperty()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"{aww"));
|
||||
Assert.IsTrue(reader.Read());
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end while parsing unquoted property name. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1193,11 +1201,6 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Invalid character after parsing property name. Expected ':' but got: "". Line 3, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void MissingColon()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -1215,7 +1218,12 @@ bye", reader.Value);
|
||||
reader.Read();
|
||||
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
|
||||
|
||||
reader.Read();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
@"Invalid character after parsing property name. Expected ':' but got: "". Path 'A', line 3, position 9.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1289,17 +1297,18 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected character encountered while parsing value: }. Line 1, position 1."
|
||||
#endif
|
||||
)]
|
||||
public void ReadBadCharInArray()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"[}"));
|
||||
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected character encountered while parsing value: }. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1321,16 +1330,16 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end when reading bytes. Line 1, position 1."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsBytesNoContentWrappedObject()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"{"));
|
||||
|
||||
reader.ReadAsBytes();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end when reading bytes. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
@@ -1345,44 +1354,43 @@ bye", reader.Value);
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading decimal. Unexpected token: StartConstructor. Line 1, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDecimalBadContent()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
|
||||
|
||||
Assert.IsNull(reader.ReadAsDecimal());
|
||||
Assert.AreEqual(JsonToken.None, reader.TokenType);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading decimal. Unexpected token: StartConstructor. Path '', line 1, position 9.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDecimal();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading bytes. Unexpected token: StartConstructor. Line 1, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsBytesBadContent()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
|
||||
|
||||
reader.ReadAsBytes();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading bytes. Unexpected token: StartConstructor. Path '', line 1, position 9.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading date. Unexpected token: StartConstructor. Line 1, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDateTimeOffsetBadContent()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()"));
|
||||
|
||||
reader.ReadAsDateTimeOffset();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading date. Unexpected token: StartConstructor. Path '', line 1, position 9.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDateTimeOffset();
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1398,29 +1406,29 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end when reading bytes. Line 1, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsBytesIntegerArrayWithNoEnd()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"[1"));
|
||||
|
||||
reader.ReadAsBytes();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end when reading bytes. Path '[0]', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected token when reading bytes: Float. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsBytesArrayWithBadContent()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"[1.0]"));
|
||||
|
||||
reader.ReadAsBytes();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected token when reading bytes: Float. Path '[0]', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1515,11 +1523,6 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not convert string to DateTimeOffset: blablahbla. Line 1, position 22."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDateTimeOffsetBadString()
|
||||
{
|
||||
string json = @"{""Offset"":""blablahbla""}";
|
||||
@@ -1532,7 +1535,12 @@ bye", reader.Value);
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsDateTimeOffset();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to DateTimeOffset: blablahbla. Path 'Offset', line 1, position 22.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDateTimeOffset();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1672,11 +1680,6 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(FormatException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Input string was not in a correct format."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsIntDecimal()
|
||||
{
|
||||
string json = @"{""Name"": 1.1}";
|
||||
@@ -1689,70 +1692,77 @@ bye", reader.Value);
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
|
||||
Assert.AreEqual(typeof(int), reader.ValueType);
|
||||
Assert.AreEqual(1, reader.Value);
|
||||
ExceptionAssert.Throws<FormatException>(
|
||||
"Input string was not in a correct format.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsInt32();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error parsing null value. Line 0, position 0."
|
||||
#endif
|
||||
)]
|
||||
public void MatchWithInsufficentCharacters()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"nul"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error parsing null value. Path '', line 0, position 0.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error parsing null value. Line 0, position 0."
|
||||
#endif
|
||||
)]
|
||||
public void MatchWithWrongCharacters()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"nulz"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error parsing null value. Path '', line 0, position 0.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error parsing null value. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void MatchWithNoTrailingSeperator()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"nullz"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error parsing null value. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end while parsing comment. Line 1, position 6."
|
||||
#endif
|
||||
)]
|
||||
public void UnclosedComment()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"/* sdf"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end while parsing comment. Path '', line 1, position 6.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error parsing comment. Expected: *, got s. Line 1, position 1."
|
||||
#endif
|
||||
)]
|
||||
public void BadCommentStart()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(@"/sdf"));
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error parsing comment. Expected: *, got s. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1924,31 +1934,31 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end while parsing constructor. Line 1, position 7."
|
||||
#endif
|
||||
)]
|
||||
public void ParseConstructorWithUnexpectedEnd()
|
||||
{
|
||||
string json = "new Dat";
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
reader.Read();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end while parsing constructor. Path '', line 1, position 7.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected character while parsing constructor: !. Line 1, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void ParseConstructorWithUnexpectedCharacter()
|
||||
{
|
||||
string json = "new Date !";
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
reader.Read();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected character while parsing constructor: !. Path '', line 1, position 9.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1990,16 +2000,16 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error parsing boolean value. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void ParseIncompleteCommentSeperator()
|
||||
{
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader("true/"));
|
||||
|
||||
reader.Read();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error parsing boolean value. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2016,17 +2026,17 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected character while parsing constructor: ,. Line 1, position 8."
|
||||
#endif
|
||||
)]
|
||||
public void ParseConstructorWithBadCharacter()
|
||||
{
|
||||
string json = "new Date,()";
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected character while parsing constructor: ,. Path '', line 1, position 8.",
|
||||
() =>
|
||||
{
|
||||
Assert.IsTrue(reader.Read());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2181,17 +2191,18 @@ bye", reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional text encountered after finished reading JSON content: }. Line 1, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEndTokenWhenParsingOddEndToken()
|
||||
{
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"{}}"));
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsTrue(reader.Read());
|
||||
reader.Read();
|
||||
JsonReader reader = new JsonTextReader(new StringReader(@"{}}"));
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsTrue(reader.Read());
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: }. Path '', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
reader.Read();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2275,7 +2286,7 @@ bye", reader.Value);
|
||||
Assert.IsTrue(reader.Read());
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"The reader's MaxDepth of 1 has been exceeded. Line 1, position 2.",
|
||||
"The reader's MaxDepth of 1 has been exceeded. Path '[0]', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
Assert.IsTrue(reader.Read());
|
||||
@@ -2296,7 +2307,7 @@ bye", reader.Value);
|
||||
Assert.AreEqual(0, reader.Depth);
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"The reader's MaxDepth of 1 has been exceeded. Line 1, position 2.",
|
||||
"The reader's MaxDepth of 1 has been exceeded. Path '[0]', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
Assert.IsTrue(reader.Read());
|
||||
@@ -2319,7 +2330,7 @@ bye", reader.Value);
|
||||
Assert.AreEqual(1, reader.Depth);
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"The reader's MaxDepth of 1 has been exceeded. Line 1, position 9.",
|
||||
"The reader's MaxDepth of 1 has been exceeded. Path '[1]', line 1, position 9.",
|
||||
() =>
|
||||
{
|
||||
Assert.IsTrue(reader.Read());
|
||||
|
||||
@@ -172,20 +172,20 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation."
|
||||
#endif
|
||||
)]
|
||||
public void WriteValueObjectWithUnsupportedValue()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(new Version(1, 1, 1, 1));
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.",
|
||||
() =>
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(new Version(1, 1, 1, 1));
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -594,43 +594,43 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonWriterException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "No token to close."
|
||||
#endif
|
||||
)]
|
||||
public void BadWriteEndArray()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
ExceptionAssert.Throws<JsonWriterException>(
|
||||
"No token to close.",
|
||||
() =>
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
|
||||
jsonWriter.WriteValue(0.0);
|
||||
jsonWriter.WriteValue(0.0);
|
||||
|
||||
jsonWriter.WriteEndArray();
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
jsonWriter.WriteEndArray();
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Invalid JavaScript string quote character. Valid quote characters are ' and ""."
|
||||
#endif
|
||||
)]
|
||||
public void InvalidQuoteChar()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"Invalid JavaScript string quote character. Valid quote characters are ' and "".",
|
||||
() =>
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.QuoteChar = '*';
|
||||
}
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
jsonWriter.QuoteChar = '*';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -86,16 +86,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not determine JSON object type for type System.String[]."
|
||||
#endif
|
||||
)]
|
||||
public void JObjectPropertyNameWithNonToken()
|
||||
{
|
||||
dynamic d = new JObject();
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
"Could not determine JSON object type for type System.String[].",
|
||||
() =>
|
||||
{
|
||||
dynamic d = new JObject();
|
||||
|
||||
d.First = new [] {"One", "II", "3"};
|
||||
d.First = new[] { "One", "II", "3" };
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void Clear()
|
||||
{
|
||||
JArray a = new JArray { 1 };
|
||||
JArray a = new JArray {1};
|
||||
Assert.AreEqual(1, a.Count);
|
||||
|
||||
a.Clear();
|
||||
@@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
JValue v = new JValue(1);
|
||||
|
||||
JArray a = new JArray { v };
|
||||
JArray a = new JArray {v};
|
||||
|
||||
Assert.AreEqual(false, a.Contains(new JValue(2)));
|
||||
Assert.AreEqual(false, a.Contains(new JValue(1)));
|
||||
@@ -63,64 +63,62 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
JToken[] a = new JToken[5];
|
||||
|
||||
((ICollection<JToken>)j).CopyTo(a, 1);
|
||||
((ICollection<JToken>) j).CopyTo(a, 1);
|
||||
|
||||
Assert.AreEqual(null, a[0]);
|
||||
|
||||
Assert.AreEqual(1, (int)a[1]);
|
||||
Assert.AreEqual(1, (int) a[1]);
|
||||
|
||||
Assert.AreEqual(2, (int)a[2]);
|
||||
Assert.AreEqual(2, (int) a[2]);
|
||||
|
||||
Assert.AreEqual(3, (int)a[3]);
|
||||
Assert.AreEqual(3, (int) a[3]);
|
||||
|
||||
Assert.AreEqual(null, a[4]);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: array"
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToNullArrayShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(null, 0);
|
||||
|
||||
ExceptionAssert.Throws<ArgumentNullException>(
|
||||
@"Value cannot be null.
|
||||
Parameter name: array",
|
||||
() =>
|
||||
{
|
||||
((ICollection<JToken>) j).CopyTo(null, 0);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex"
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[1], -1);
|
||||
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex",
|
||||
() =>
|
||||
{
|
||||
((ICollection<JToken>) j).CopyTo(new JToken[1], -1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"arrayIndex is equal to or greater than the length of array."
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[1], 1);
|
||||
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"arrayIndex is equal to or greater than the length of array.",
|
||||
() =>
|
||||
{
|
||||
((ICollection<JToken>) j).CopyTo(new JToken[1], 1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array."
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToInsufficientArrayCapacity()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
@@ -128,7 +126,12 @@ Parameter name: arrayIndex"
|
||||
j.Add(new JValue(2));
|
||||
j.Add(new JValue(3));
|
||||
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.",
|
||||
() =>
|
||||
{
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -200,29 +203,31 @@ Parameter name: arrayIndex"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index is equal to or greater than Count.
|
||||
Parameter name: index"
|
||||
#endif
|
||||
)]
|
||||
public void RemoveAtOutOfRangeIndexShouldError()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.RemoveAt(0);
|
||||
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"Index is equal to or greater than Count.
|
||||
Parameter name: index",
|
||||
() =>
|
||||
{
|
||||
j.RemoveAt(0);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index is less than 0.
|
||||
Parameter name: index"
|
||||
#endif
|
||||
)]
|
||||
public void RemoveAtNegativeIndexShouldError()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.RemoveAt(-1);
|
||||
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"Index is less than 0.
|
||||
Parameter name: index",
|
||||
() =>
|
||||
{
|
||||
j.RemoveAt(-1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -292,33 +297,35 @@ Parameter name: index"
|
||||
JArray j = new JArray();
|
||||
j.Insert(0, null);
|
||||
|
||||
Assert.AreEqual(null, ((JValue)j[0]).Value);
|
||||
Assert.AreEqual(null, ((JValue) j[0]).Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index was out of range. Must be non-negative and less than the size of the collection.
|
||||
Parameter name: index"
|
||||
#endif
|
||||
)]
|
||||
public void InsertNegativeIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Insert(-1, new JValue(1));
|
||||
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"Index was out of range. Must be non-negative and less than the size of the collection.
|
||||
Parameter name: index",
|
||||
() =>
|
||||
{
|
||||
j.Insert(-1, new JValue(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index must be within the bounds of the List.
|
||||
Parameter name: index"
|
||||
#endif
|
||||
)]
|
||||
public void InsertOutOfRangeIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Insert(2, new JValue(1));
|
||||
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"Index must be within the bounds of the List.
|
||||
Parameter name: index",
|
||||
() =>
|
||||
{
|
||||
j.Insert(2, new JValue(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -344,15 +351,16 @@ Parameter name: index"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject"
|
||||
#endif
|
||||
)]
|
||||
public void Parse_ShouldThrowOnUnexpectedToken()
|
||||
{
|
||||
string json = @"{""prop"":""value""}";
|
||||
JArray.Parse(json);
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
JArray.Parse(json);
|
||||
});
|
||||
}
|
||||
|
||||
public class ListItemFields
|
||||
@@ -367,24 +375,24 @@ Parameter name: index"
|
||||
string itemZeroText = "Zero text";
|
||||
|
||||
IEnumerable<ListItemFields> t = new List<ListItemFields>
|
||||
{
|
||||
new ListItemFields { ListItemText = "First", ListItemValue = 1 },
|
||||
new ListItemFields { ListItemText = "Second", ListItemValue = 2 },
|
||||
new ListItemFields { ListItemText = "Third", ListItemValue = 3 }
|
||||
};
|
||||
{
|
||||
new ListItemFields {ListItemText = "First", ListItemValue = 1},
|
||||
new ListItemFields {ListItemText = "Second", ListItemValue = 2},
|
||||
new ListItemFields {ListItemText = "Third", ListItemValue = 3}
|
||||
};
|
||||
|
||||
JObject optionValues =
|
||||
new JObject(
|
||||
new JProperty("options",
|
||||
new JArray(
|
||||
new JObject(
|
||||
new JProperty("text", itemZeroText),
|
||||
new JProperty("value", "0")),
|
||||
new JObject(
|
||||
new JProperty("options",
|
||||
new JArray(
|
||||
new JObject(
|
||||
new JProperty("text", itemZeroText),
|
||||
new JProperty("value", "0")),
|
||||
from r in t
|
||||
orderby r.ListItemValue
|
||||
select new JObject(
|
||||
new JProperty("text", r.ListItemText),
|
||||
new JProperty("value", r.ListItemValue.ToString())))));
|
||||
new JProperty("text", r.ListItemText),
|
||||
new JProperty("value", r.ListItemValue.ToString())))));
|
||||
|
||||
string result = "myOptions = " + optionValues.ToString();
|
||||
|
||||
@@ -418,12 +426,12 @@ Parameter name: index"
|
||||
int i = 1;
|
||||
foreach (JToken token in a)
|
||||
{
|
||||
Assert.AreEqual(i, (int)token);
|
||||
Assert.AreEqual(i, (int) token);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
[Test]
|
||||
public void ITypedListGetItemProperties()
|
||||
@@ -447,21 +455,21 @@ Parameter name: index"
|
||||
a.Add(a);
|
||||
|
||||
Assert.AreEqual(3, a.Count);
|
||||
Assert.AreEqual(1, (int)a[0]);
|
||||
Assert.AreEqual(2, (int)a[1]);
|
||||
Assert.AreEqual(1, (int) a[0]);
|
||||
Assert.AreEqual(2, (int) a[1]);
|
||||
Assert.AreNotSame(a, a[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Set JArray values with invalid key value: ""badvalue"". Array position index expected."
|
||||
#endif
|
||||
)]
|
||||
public void SetValueWithInvalidIndex()
|
||||
{
|
||||
JArray a = new JArray();
|
||||
a["badvalue"] = new JValue(3);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"Set JArray values with invalid key value: ""badvalue"". Array position index expected.",
|
||||
() =>
|
||||
{
|
||||
JArray a = new JArray();
|
||||
a["badvalue"] = new JValue(3);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -469,35 +477,35 @@ Parameter name: index"
|
||||
{
|
||||
object key = 0;
|
||||
|
||||
JArray a = new JArray((object)null);
|
||||
JArray a = new JArray((object) null);
|
||||
a[key] = new JValue(3);
|
||||
|
||||
Assert.AreEqual(3, (int)a[key]);
|
||||
Assert.AreEqual(3, (int) a[key]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReplaceAll()
|
||||
{
|
||||
JArray a = new JArray(new [] { 1, 2, 3 });
|
||||
JArray a = new JArray(new[] {1, 2, 3});
|
||||
Assert.AreEqual(3, a.Count);
|
||||
Assert.AreEqual(1, (int)a[0]);
|
||||
Assert.AreEqual(2, (int)a[1]);
|
||||
Assert.AreEqual(3, (int)a[2]);
|
||||
Assert.AreEqual(1, (int) a[0]);
|
||||
Assert.AreEqual(2, (int) a[1]);
|
||||
Assert.AreEqual(3, (int) a[2]);
|
||||
|
||||
a.ReplaceAll(1);
|
||||
Assert.AreEqual(1, a.Count);
|
||||
Assert.AreEqual(1, (int)a[0]);
|
||||
Assert.AreEqual(1, (int) a[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected end of content while loading JArray."
|
||||
#endif
|
||||
)]
|
||||
public void ParseIncomplete()
|
||||
{
|
||||
JArray.Parse("[1");
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Unexpected end of content while loading JArray. Path '[0]', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
JArray.Parse("[1");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -508,16 +516,11 @@ Parameter name: index"
|
||||
array.Insert(1, 456);
|
||||
|
||||
Assert.AreEqual(2, array.Count);
|
||||
Assert.AreEqual(123, (int)array[0]);
|
||||
Assert.AreEqual(456, (int)array[1]);
|
||||
Assert.AreEqual(123, (int) array[0]);
|
||||
Assert.AreEqual(456, (int) array[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional text encountered after finished reading JSON content: ,. Line 5, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void ParseAdditionalContent()
|
||||
{
|
||||
string json = @"[
|
||||
@@ -526,7 +529,12 @@ Parameter name: index"
|
||||
""Large""
|
||||
], 987987";
|
||||
|
||||
JArray.Parse(json);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
|
||||
() =>
|
||||
{
|
||||
JArray.Parse(json);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,16 +113,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object."
|
||||
#endif
|
||||
)]
|
||||
public void DuplicatePropertyNameShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", null);
|
||||
o.Add("PropertyNameValue", null);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
"Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", null);
|
||||
o.Add("PropertyNameValue", null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -227,57 +227,57 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: array"
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToNullArrayShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"Value cannot be null.
|
||||
Parameter name: array",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex"
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
|
||||
ExceptionAssert.Throws<ArgumentOutOfRangeException>(
|
||||
@"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"arrayIndex is equal to or greater than the length of array."
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"arrayIndex is equal to or greater than the length of array.",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array."
|
||||
#endif
|
||||
)]
|
||||
public void GenericCollectionCopyToInsufficientArrayCapacity()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
o.Add("PropertyNameValue2", new JValue(2));
|
||||
o.Add("PropertyNameValue3", new JValue(3));
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
o.Add("PropertyNameValue2", new JValue(2));
|
||||
o.Add("PropertyNameValue3", new JValue(3));
|
||||
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -363,7 +363,7 @@ Parameter name: arrayIndex"
|
||||
[Test]
|
||||
public void Parse_ShouldThrowOnUnexpectedToken()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray",
|
||||
ExceptionAssert.Throws<JsonReaderException>("Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
string json = @"[""prop""]";
|
||||
@@ -400,15 +400,13 @@ Parameter name: arrayIndex"
|
||||
public void Blog()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Invalid property identifier character: ]. Line 3, position 5.",
|
||||
"Invalid property identifier character: ]. Path 'name', line 3, position 5.",
|
||||
() =>
|
||||
{
|
||||
JObject person = JObject.Parse(@"{
|
||||
JObject.Parse(@"{
|
||||
""name"": ""James"",
|
||||
]!#$THIS IS: BAD JSON![{}}}}]
|
||||
}");
|
||||
|
||||
// Invalid property identifier character: ]. Line 3, position 9.
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1271,19 +1269,27 @@ Parameter name: arrayIndex"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(NotSupportedException))]
|
||||
public void IBindingListApplySort()
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.ApplySort(null, ListSortDirection.Ascending);
|
||||
ExceptionAssert.Throws<NotSupportedException>(
|
||||
"Specified method is not supported.",
|
||||
() =>
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.ApplySort(null, ListSortDirection.Ascending);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(NotSupportedException))]
|
||||
public void IBindingListRemoveSort()
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.RemoveSort();
|
||||
ExceptionAssert.Throws<NotSupportedException>(
|
||||
"Specified method is not supported.",
|
||||
() =>
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.RemoveSort();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1295,11 +1301,15 @@ Parameter name: arrayIndex"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(NotSupportedException))]
|
||||
public void IBindingListFind()
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.Find(null, null);
|
||||
ExceptionAssert.Throws<NotSupportedException>(
|
||||
"Specified method is not supported.",
|
||||
() =>
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.Find(null, null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1310,11 +1320,15 @@ Parameter name: arrayIndex"
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.")]
|
||||
public void IBindingListAddNew()
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.AddNew();
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.",
|
||||
() =>
|
||||
{
|
||||
IBindingList l = new JObject();
|
||||
l.AddNew();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1580,7 +1594,7 @@ Parameter name: arrayIndex"
|
||||
[Test]
|
||||
public void NumberTooBigForInt64()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Line 1, position 30.",
|
||||
ExceptionAssert.Throws<JsonReaderException>("JSON integer 307953220000517141511 is too large or small for an Int64. Path 'code', line 1, position 30.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{""code"": 307953220000517141511}";
|
||||
@@ -1592,7 +1606,7 @@ Parameter name: arrayIndex"
|
||||
[Test]
|
||||
public void ParseIncomplete()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject.",
|
||||
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject. Path 'foo', line 1, position 6.",
|
||||
() =>
|
||||
{
|
||||
JObject.Parse("{ foo:");
|
||||
@@ -1631,7 +1645,7 @@ Parameter name: arrayIndex"
|
||||
[Test]
|
||||
public void LoadFromNestedObjectIncomplete()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>("Unexpected end of content while loading JObject.",
|
||||
ExceptionAssert.Throws<JsonReaderException>("Unexpected end of content while loading JObject. Path 'short.error.code', line 6, position 15.",
|
||||
() =>
|
||||
{
|
||||
string jsonText = @"{
|
||||
@@ -1729,7 +1743,7 @@ Parameter name: arrayIndex"
|
||||
[Test]
|
||||
public void ParseAdditionalContent()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Line 10, position 2.",
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 10, position 2.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
|
||||
@@ -59,25 +59,25 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Unexpected character while parsing path indexer: ["
|
||||
#endif
|
||||
)]
|
||||
public void BadCharactersInIndexer()
|
||||
{
|
||||
new JPath("Blah[[0]].Two.Three[1].Four");
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
@"Unexpected character while parsing path indexer: [",
|
||||
() =>
|
||||
{
|
||||
new JPath("Blah[[0]].Two.Three[1].Four");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Path ended with open indexer. Expected ]"
|
||||
#endif
|
||||
)]
|
||||
public void UnclosedIndexer()
|
||||
{
|
||||
new JPath("Blah[0");
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
@"Path ended with open indexer. Expected ]",
|
||||
() =>
|
||||
{
|
||||
new JPath("Blah[0");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -102,25 +102,25 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Empty path indexer."
|
||||
#endif
|
||||
)]
|
||||
public void EmptyIndexer()
|
||||
{
|
||||
new JPath("[]");
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Empty path indexer.",
|
||||
() =>
|
||||
{
|
||||
new JPath("[]");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected character while parsing path: ]"
|
||||
#endif
|
||||
)]
|
||||
public void IndexerCloseInProperty()
|
||||
{
|
||||
new JPath("]");
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Unexpected character while parsing path: ]",
|
||||
() =>
|
||||
{
|
||||
new JPath("]");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -135,14 +135,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unexpected character following indexer: B"
|
||||
#endif
|
||||
)]
|
||||
public void MissingDotAfterIndexer()
|
||||
{
|
||||
new JPath("[1]Blah");
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Unexpected character following indexer: B",
|
||||
() =>
|
||||
{
|
||||
new JPath("[1]Blah");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -178,17 +178,17 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index 1 not valid on JObject."
|
||||
#endif
|
||||
)]
|
||||
public void EvaluateIndexerOnObjectWithError()
|
||||
{
|
||||
JObject o = new JObject(
|
||||
new JProperty("Blah", 1));
|
||||
|
||||
o.SelectToken("[1]", true);
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
@"Index 1 not valid on JObject.",
|
||||
() =>
|
||||
{
|
||||
o.SelectToken("[1]", true);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -201,43 +201,43 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Property 'BlahBlah' not valid on JArray."
|
||||
#endif
|
||||
)]
|
||||
public void EvaluatePropertyOnArrayWithError()
|
||||
{
|
||||
JArray a = new JArray(1, 2, 3, 4, 5);
|
||||
|
||||
a.SelectToken("BlahBlah", true);
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
@"Property 'BlahBlah' not valid on JArray.",
|
||||
() =>
|
||||
{
|
||||
a.SelectToken("BlahBlah", true);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(IndexOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Index 1 outside the bounds of JConstructor."
|
||||
#endif
|
||||
)]
|
||||
public void EvaluateConstructorOutOfBoundsIndxerWithError()
|
||||
{
|
||||
JConstructor c = new JConstructor("Blah");
|
||||
|
||||
c.SelectToken("[1]", true);
|
||||
ExceptionAssert.Throws<IndexOutOfRangeException>(
|
||||
@"Index 1 outside the bounds of JConstructor.",
|
||||
() =>
|
||||
{
|
||||
c.SelectToken("[1]", true);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Property 'Missing' does not exist on JObject."
|
||||
#endif
|
||||
)]
|
||||
public void EvaluateMissingPropertyWithError()
|
||||
{
|
||||
JObject o = new JObject(
|
||||
new JProperty("Blah", 1));
|
||||
|
||||
o.SelectToken("Missing", true);
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Property 'Missing' does not exist on JObject.",
|
||||
() =>
|
||||
{
|
||||
o.SelectToken("Missing", true);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -250,16 +250,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(IndexOutOfRangeException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Index 1000 outside the bounds of JArray."
|
||||
#endif
|
||||
)]
|
||||
public void EvaluateArrayOutOfBoundsIndxerWithError()
|
||||
{
|
||||
JArray a = new JArray(1, 2, 3, 4, 5);
|
||||
|
||||
a.SelectToken("[1000].Ha", true);
|
||||
ExceptionAssert.Throws<IndexOutOfRangeException>(
|
||||
"Index 1000 outside the bounds of JArray.",
|
||||
() =>
|
||||
{
|
||||
a.SelectToken("[1000].Ha", true);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -42,15 +42,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
int? index = null;
|
||||
|
||||
l.ListChanged += (sender, args) =>
|
||||
{
|
||||
listChangedType = args.ListChangedType;
|
||||
index = args.NewIndex;
|
||||
};
|
||||
{
|
||||
listChangedType = args.ListChangedType;
|
||||
index = args.NewIndex;
|
||||
};
|
||||
|
||||
p.Value = 1;
|
||||
|
||||
Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
|
||||
Assert.AreEqual(0, index.Value);
|
||||
Assert.AreEqual(0, index.Value);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -64,45 +64,45 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
|
||||
#endif
|
||||
)]
|
||||
public void IListClear()
|
||||
{
|
||||
JProperty p = new JProperty("TestProperty", null);
|
||||
IList l = p;
|
||||
|
||||
l.Clear();
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
|
||||
() =>
|
||||
{
|
||||
l.Clear();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
|
||||
#endif
|
||||
)]
|
||||
public void IListAdd()
|
||||
{
|
||||
JProperty p = new JProperty("TestProperty", null);
|
||||
IList l = p;
|
||||
|
||||
l.Add(null);
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
|
||||
() =>
|
||||
{
|
||||
l.Add(null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
|
||||
#endif
|
||||
)]
|
||||
public void IListRemove()
|
||||
{
|
||||
JProperty p = new JProperty("TestProperty", null);
|
||||
IList l = p;
|
||||
|
||||
l.Remove(p.Value);
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
|
||||
() =>
|
||||
{
|
||||
l.Remove(p.Value);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -136,25 +136,25 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void MultiContentConstructor()
|
||||
{
|
||||
JProperty p = new JProperty("error", new List<string> { "one", "two" });
|
||||
JProperty p = new JProperty("error", new List<string> {"one", "two"});
|
||||
JArray a = (JArray) p.Value;
|
||||
|
||||
Assert.AreEqual(a.Count, 2);
|
||||
Assert.AreEqual("one", (string)a[0]);
|
||||
Assert.AreEqual("two", (string)a[1]);
|
||||
Assert.AreEqual("one", (string) a[0]);
|
||||
Assert.AreEqual("two", (string) a[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
|
||||
#endif
|
||||
)]
|
||||
public void IListGenericAdd()
|
||||
{
|
||||
IList<JToken> t = new JProperty("error", new List<string> { "one", "two" });
|
||||
t.Add(1);
|
||||
t.Add(2);
|
||||
IList<JToken> t = new JProperty("error", new List<string> {"one", "two"});
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
|
||||
() =>
|
||||
{
|
||||
t.Add(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,11 +123,6 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not convert string to DateTimeOffset: blablahbla. Line 1, position 22."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDateTimeOffsetBadString()
|
||||
{
|
||||
string json = @"{""Offset"":""blablahbla""}";
|
||||
@@ -142,15 +137,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsDateTimeOffset();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to DateTimeOffset: blablahbla. Path 'Offset', line 1, position 22.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDateTimeOffset();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading date. Unexpected token: Boolean. Line 1, position 14."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDateTimeOffsetBoolean()
|
||||
{
|
||||
string json = @"{""Offset"":true}";
|
||||
@@ -165,7 +160,12 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsDateTimeOffset();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading date. Unexpected token: Boolean. Path 'Offset', line 1, position 14.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDateTimeOffset();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -311,26 +311,27 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void ReadBytesFailure()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("Error reading bytes. Unexpected token: Integer.",
|
||||
() =>
|
||||
{
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", 1)
|
||||
);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading bytes. Unexpected token: Integer. Path 'Test1'.",
|
||||
() =>
|
||||
{
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", 1)
|
||||
);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
jsonReader.ReadAsBytes();
|
||||
}
|
||||
});
|
||||
jsonReader.ReadAsBytes();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public class HasBytes
|
||||
@@ -480,11 +481,6 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not convert string to integer: hi. Line 1, position 12."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsInt32BadString()
|
||||
{
|
||||
string json = @"{""Name"":""hi""}";
|
||||
@@ -499,18 +495,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
|
||||
Assert.AreEqual(typeof(int), reader.ValueType);
|
||||
Assert.AreEqual(1, reader.Value);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to integer: hi. Path 'Name', line 1, position 12.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsInt32();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading integer. Unexpected token: Boolean. Line 1, position 12."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsInt32Boolean()
|
||||
{
|
||||
string json = @"{""Name"":true}";
|
||||
@@ -525,7 +518,12 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading integer. Unexpected token: Boolean. Path 'Name', line 1, position 12.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsInt32();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -550,11 +548,6 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not convert string to decimal: blah. Line 1, position 14."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDecimalBadString()
|
||||
{
|
||||
string json = @"{""Name"":""blah""}";
|
||||
@@ -569,15 +562,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsDecimal();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to decimal: blah. Path 'Name', line 1, position 14.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDecimal();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error reading decimal. Unexpected token: Boolean. Line 1, position 12."
|
||||
#endif
|
||||
)]
|
||||
public void ReadAsDecimalBoolean()
|
||||
{
|
||||
string json = @"{""Name"":true}";
|
||||
@@ -592,7 +585,12 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
reader.ReadAsDecimal();
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading decimal. Unexpected token: Boolean. Path 'Name', line 1, position 12.",
|
||||
() =>
|
||||
{
|
||||
reader.ReadAsDecimal();
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -565,15 +565,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject."
|
||||
#endif
|
||||
)]
|
||||
public void AddValueToObject()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add(5);
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
|
||||
() =>
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add(5);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -770,7 +770,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void ParseAdditionalContent()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Line 5, position 2.",
|
||||
ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
|
||||
() =>
|
||||
{
|
||||
string json = @"[
|
||||
|
||||
@@ -72,5 +72,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.3.14818")]
|
||||
[assembly: AssemblyFileVersion("4.5.3.14821")]
|
||||
#endif
|
||||
|
||||
@@ -188,7 +188,7 @@ 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. Line 2, position 18.",
|
||||
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();
|
||||
|
||||
@@ -391,14 +391,14 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"A member with the name 'pie' already exists on 'Newtonsoft.Json.Tests.TestObjects.BadJsonPropertyClass'. Use the JsonPropertyAttribute to specify another name."
|
||||
#endif
|
||||
)]
|
||||
public void BadJsonPropertyClassSerialize()
|
||||
{
|
||||
JsonConvert.SerializeObject(new BadJsonPropertyClass());
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -469,16 +469,16 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Bad JSON escape sequence: \j. Line 1, position 7."
|
||||
#endif
|
||||
)]
|
||||
public void InvalidBackslash()
|
||||
{
|
||||
string json = @"[""vvv\jvvv""]";
|
||||
|
||||
JsonConvert.DeserializeObject<List<string>>(json);
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
@"Bad JSON escape sequence: \j. Path '', line 1, position 7.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<List<string>>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -933,7 +933,7 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(@"{""DefaultConverter"":""\/Date(0)\/"",""MemberConverter"":""1970-01-01T00:00:00Z""}", json);
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Could not convert string to DateTime: /Date(0)/. Line 1, position 33.",
|
||||
"Could not convert string to DateTime: /Date(0)/. Path 'DefaultConverter', line 1, position 33.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemberConverterClass>(json, new JsonSerializerSettings
|
||||
@@ -1178,51 +1178,54 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void DeserializeRequiredMembersClassNullRequiredValueProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Required property 'FirstName' expects a value but got null. Line 6, position 2.",
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Required property 'FirstName' expects a value but got null. Path '', line 6, position 2.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
{
|
||||
string json = @"{
|
||||
""FirstName"": null,
|
||||
""MiddleName"": null,
|
||||
""LastName"": null,
|
||||
""BirthDate"": ""\/Date(977309755000)\/""
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<RequiredMembersClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<RequiredMembersClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRequiredMembersClassNullRequiredValueProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Cannot write a null value for property 'FirstName'. Property requires a value.",
|
||||
() =>
|
||||
{
|
||||
RequiredMembersClass requiredMembersClass = new RequiredMembersClass
|
||||
{
|
||||
FirstName = null,
|
||||
BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
|
||||
LastName = null,
|
||||
MiddleName = null
|
||||
};
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot write a null value for property 'FirstName'. Property requires a value.",
|
||||
() =>
|
||||
{
|
||||
RequiredMembersClass requiredMembersClass = new RequiredMembersClass
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RequiredMembersClassMissingRequiredProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Required property 'LastName' not found in JSON. Line 3, position 2.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Required property 'LastName' not found in JSON. Path '', line 3, position 2.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
""FirstName"": ""Bob""
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<RequiredMembersClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<RequiredMembersClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1301,17 +1304,18 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Could not create an instance of type Newtonsoft.Json.Tests.TestObjects.ICo. Type is an interface or abstract class and cannot be instantated. Line 1, position 14."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeInterfaceProperty()
|
||||
{
|
||||
InterfacePropertyTestClass testClass = new InterfacePropertyTestClass();
|
||||
testClass.co = new Co();
|
||||
String strFromTest = JsonConvert.SerializeObject(testClass);
|
||||
InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass) JsonConvert.DeserializeObject(strFromTest, typeof (InterfacePropertyTestClass));
|
||||
|
||||
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 instantated. Path 'co.Name', line 1, position 14.",
|
||||
() =>
|
||||
{
|
||||
InterfacePropertyTestClass testFromDe = (InterfacePropertyTestClass) JsonConvert.DeserializeObject(strFromTest, typeof (InterfacePropertyTestClass));
|
||||
});
|
||||
}
|
||||
|
||||
private Person GetPerson()
|
||||
@@ -1471,13 +1475,19 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException))]
|
||||
public void JsonPropertyWithHandlingValues_ReferenceLoopError()
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
o.ReferenceLoopHandlingErrorProperty = o;
|
||||
string classRef = typeof(JsonPropertyWithHandlingValues).FullName;
|
||||
|
||||
JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings {ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Self referencing loop detected for type '" + classRef + "'.",
|
||||
() =>
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
o.ReferenceLoopHandlingErrorProperty = o;
|
||||
|
||||
JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -1957,16 +1967,16 @@ keyword such as type of business.""
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"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. Line 1, position 15."
|
||||
#endif
|
||||
)]
|
||||
public void FailWhenClassWithNoDefaultConstructorHasMultipleConstructorsWithArguments()
|
||||
{
|
||||
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)\/""}";
|
||||
|
||||
Event e = JsonConvert.DeserializeObject<Event>(json);
|
||||
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.",
|
||||
() =>
|
||||
{
|
||||
Event e = JsonConvert.DeserializeObject<Event>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2001,59 +2011,59 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Cannot deserialize JSON array (i.e. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person'.
|
||||
The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.
|
||||
To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Line 1, position 1."
|
||||
#endif
|
||||
)]
|
||||
public void CannotDeserializeArrayIntoObject()
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
JsonConvert.DeserializeObject<Person>(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize JSON array (i.e. [1,2,3]) into type 'Newtonsoft.Json.Tests.TestObjects.Person'.
|
||||
The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.
|
||||
To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<Person>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Cannot deserialize JSON object (i.e. {""name"":""value""}) into type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'.
|
||||
The deserialized type should be a normal .NET type (i.e. not a primitive type like integer, not a collection type like an array or List<T>) or a dictionary type (i.e. Dictionary<TKey, TValue>).
|
||||
To force JSON objects to deserialize add the JsonObjectAttribute to the type. Line 1, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void CannotDeserializeObjectIntoArray()
|
||||
{
|
||||
string json = @"{}";
|
||||
|
||||
JsonConvert.DeserializeObject<List<Person>>(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot deserialize JSON object (i.e. {""name"":""value""}) into type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'.
|
||||
The deserialized type should be a normal .NET type (i.e. not a primitive type like integer, not a collection type like an array or List<T>) or a dictionary type (i.e. Dictionary<TKey, TValue>).
|
||||
To force JSON objects to deserialize add the JsonObjectAttribute to the type. Path '', line 1, position 2.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<List<Person>>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Cannot populate JSON array onto type 'Newtonsoft.Json.Tests.TestObjects.Person'. Line 1, position 1."
|
||||
#endif
|
||||
)]
|
||||
public void CannotPopulateArrayIntoObject()
|
||||
{
|
||||
string json = @"[]";
|
||||
|
||||
JsonConvert.PopulateObject(json, new Person());
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot populate JSON array onto type 'Newtonsoft.Json.Tests.TestObjects.Person'. Path '', line 1, position 1.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.PopulateObject(json, new Person());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Cannot populate JSON object onto type 'System.Collections.Generic.List`1[Newtonsoft.Json.Tests.TestObjects.Person]'. Line 1, position 2."
|
||||
#endif
|
||||
)]
|
||||
public void CannotPopulateObjectIntoArray()
|
||||
{
|
||||
string json = @"{}";
|
||||
|
||||
JsonConvert.PopulateObject(json, new List<Person>());
|
||||
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>());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2066,79 +2076,79 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'."
|
||||
#endif
|
||||
)]
|
||||
public void SerializePropertyGetError()
|
||||
{
|
||||
JsonConvert.SerializeObject(new MemoryStream(), new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(new MemoryStream(), new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Error setting value to 'ReadTimeout' on 'System.IO.MemoryStream'."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializePropertySetError()
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:0}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonReaderException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Could not convert string to integer: . Line 1, position 15."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeEnsureTypeEmptyStringToIntError()
|
||||
public void DeserializePropertySetError()
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error setting value to 'ReadTimeout' on 'System.IO.MemoryStream'.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:0}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Error converting value {null} to type 'System.Int32'. Line 1, position 17."
|
||||
public void DeserializeEnsureTypeEmptyStringToIntError()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
@"Could not convert string to integer: . Path 'ReadTimeout', line 1, position 15.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:''}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
)]
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnsureTypeNullToIntError()
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Error converting value {null} to type 'System.Int32'. Path 'ReadTimeout', line 1, position 17.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<MemoryStream>("{ReadTimeout:null}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -2252,7 +2262,7 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li
|
||||
[Test]
|
||||
public void DeserializePersonKeyedDictionary()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type 'Newtonsoft.Json.Tests.TestObjects.Person'. Create a TypeConverter to convert from the string to the key type object. Line 2, position 46.",
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type 'Newtonsoft.Json.Tests.TestObjects.Person'. Create a TypeConverter to convert from the string to the key type object. Path 'Newtonsoft.Json.Tests.TestObjects.Person', line 2, position 46.",
|
||||
() =>
|
||||
{
|
||||
string json =
|
||||
@@ -2518,15 +2528,16 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Unable to find a default constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor. Line 1, position 6."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeDictionaryWithNoDefaultConstructor()
|
||||
{
|
||||
string json = "{key1:'value',key2:'value',key3:'value'}";
|
||||
JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unable to find a default constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor. Path 'key1', line 1, position 6.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json);
|
||||
});
|
||||
}
|
||||
|
||||
public class DictionaryWithNoDefaultConstructor : Dictionary<string, string>
|
||||
@@ -2913,7 +2924,7 @@ To force JSON objects to deserialize add the JsonObjectAttribute to the type. Li
|
||||
{
|
||||
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 to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Line 1, position 14.",
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to 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);
|
||||
@@ -3608,11 +3619,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Additional content found in JSON reference object. A JSON reference object should only have a $ref property. Line 6, position 11."
|
||||
#endif
|
||||
)]
|
||||
public void SerializeRefAdditionalContent()
|
||||
{
|
||||
//Additional text found in JSON string after finishing deserializing object.
|
||||
@@ -3627,13 +3633,20 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
child.Add("Father", reference);
|
||||
|
||||
var json = JsonConvert.SerializeObject(child, Formatting.Indented);
|
||||
JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRefBadType()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("JSON reference $ref property must have a string or null value. Line 5, position 14.",
|
||||
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.
|
||||
@@ -4071,9 +4084,10 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (Exception), ExpectedMessage = "Cannot create and populate list type System.Collections.Specialized.StringDictionary.")]
|
||||
public void StringDictionaryTest()
|
||||
{
|
||||
string classRef = typeof(StringDictionary).FullName;
|
||||
|
||||
StringDictionaryTestClass s1 = new StringDictionaryTestClass()
|
||||
{
|
||||
StringDictionaryProperty = new StringDictionary()
|
||||
@@ -4086,7 +4100,12 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
|
||||
string json = JsonConvert.SerializeObject(s1, Formatting.Indented);
|
||||
|
||||
JsonConvert.DeserializeObject<StringDictionaryTestClass>(json);
|
||||
ExceptionAssert.Throws<InvalidOperationException>(
|
||||
"Cannot create and populate list type " + classRef + ".",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<StringDictionaryTestClass>(json);
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4649,14 +4668,14 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error converting value {null} to type 'System.DateTime'. Line 1, position 4."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeNullDateTimeValueTest()
|
||||
{
|
||||
JsonConvert.DeserializeObject("null", typeof (DateTime));
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Error converting value {null} to type 'System.DateTime'. Path '', line 1, position 4.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject("null", typeof (DateTime));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -5360,16 +5379,16 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (Exception)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not deserialize Null to KeyValuePair."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeNullToNonNullableKeyValuePairArray()
|
||||
{
|
||||
string json = @"[ null ]";
|
||||
|
||||
JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot convert null value to KeyValuePair. Path '[0]', line 1, position 6.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -5508,11 +5527,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Error converting value {null} to type 'System.Int32'. Line 5, position 7."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeNullInt()
|
||||
{
|
||||
string json = @"[
|
||||
@@ -5522,8 +5536,12 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
null
|
||||
]";
|
||||
|
||||
List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json);
|
||||
// JsonSerializationException : Error converting value {null} to type 'System.Int32'. Line 5, position 7.
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -5551,29 +5569,31 @@ 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. Line 2, position 22.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
ExceptionAssert.Throws<JsonReaderException>(
|
||||
"Error reading integer. Unexpected token: Boolean. Path 'PreProperty', line 2, position 22.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
""PreProperty"": true,
|
||||
""PostProperty"": ""-1""
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeUnexpectedEndInt()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>("Unexpected end when setting PreProperty's value. Line 2, position 18.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unexpected end when setting PreProperty's value. Path 'PreProperty', line 2, position 18.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
""PreProperty"": ";
|
||||
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
JsonConvert.DeserializeObject<TestObjects.MyClass>(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -5718,36 +5738,36 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "No JSON content found and type 'System.Double' is not nullable. Line 0, position 0."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeDoubleFromEmptyString()
|
||||
{
|
||||
JsonConvert.DeserializeObject<double>("");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.Double' is not nullable. Path '', line 0, position 0.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<double>("");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "No JSON content found and type 'System.StringComparison' is not nullable. Line 0, position 0."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeEnumFromEmptyString()
|
||||
{
|
||||
JsonConvert.DeserializeObject<StringComparison>("");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.StringComparison' is not nullable. Path '', line 0, position 0.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<StringComparison>("");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "No JSON content found and type 'System.Int32' is not nullable. Line 0, position 0."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeInt32FromEmptyString()
|
||||
{
|
||||
JsonConvert.DeserializeObject<int>("");
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"No JSON content found and type 'System.Int32' is not nullable. Path '', line 0, position 0.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<int>("");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -5758,27 +5778,28 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (ArgumentNullException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: value"
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeDoubleFromNullString()
|
||||
{
|
||||
JsonConvert.DeserializeObject<double>(null);
|
||||
ExceptionAssert.Throws<ArgumentNullException>(
|
||||
@"Value cannot be null.
|
||||
Parameter name: value",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<double>(null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (ArgumentNullException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: value"
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeFromNullString()
|
||||
{
|
||||
JsonConvert.DeserializeObject(null);
|
||||
ExceptionAssert.Throws<ArgumentNullException>(
|
||||
@"Value cannot be null.
|
||||
Parameter name: value",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(null);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -41,11 +41,6 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
public class MissingMemberHandlingTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Could not find member 'Price' on object of type 'ProductShort'. Line 4, position 11."
|
||||
#endif
|
||||
)]
|
||||
public void MissingMemberDeserialize()
|
||||
{
|
||||
Product product = new Product();
|
||||
@@ -67,7 +62,12 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
// ]
|
||||
//}
|
||||
|
||||
ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
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 });
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -126,16 +126,16 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Could not find member 'Missing' on object of type 'DoubleClass'. Line 1, position 11."
|
||||
#endif
|
||||
)]
|
||||
public void MissingMemeber()
|
||||
{
|
||||
string json = @"{""Missing"":1}";
|
||||
|
||||
JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
|
||||
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 });
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -140,7 +140,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. Line 1, position 1.",
|
||||
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());
|
||||
|
||||
@@ -94,15 +94,21 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException))]
|
||||
public void SerializeCircularListsError()
|
||||
{
|
||||
string classRef = typeof(CircularList).FullName;
|
||||
|
||||
CircularList circularList = new CircularList();
|
||||
circularList.Add(null);
|
||||
circularList.Add(new CircularList { null });
|
||||
circularList.Add(new CircularList { new CircularList { circularList } });
|
||||
|
||||
JsonConvert.SerializeObject(circularList, Formatting.Indented);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected for type '" + classRef + "'.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(circularList, Formatting.Indented);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -207,11 +213,6 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Cannot preserve reference to array or readonly list: System.String[][]. Line 3, position 15."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeArraysWithPreserveObjectReferences()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -240,8 +241,13 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
]
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Cannot preserve reference to array or readonly list: System.String[][]. Path '$values', line 3, position 15.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
});
|
||||
}
|
||||
|
||||
public class CircularDictionary : Dictionary<string, CircularDictionary>
|
||||
@@ -249,14 +255,20 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException))]
|
||||
public void SerializeCircularDictionarysError()
|
||||
{
|
||||
string classRef = typeof(CircularDictionary).FullName;
|
||||
|
||||
CircularDictionary circularDictionary = new CircularDictionary();
|
||||
circularDictionary.Add("other", new CircularDictionary { { "blah", null } });
|
||||
circularDictionary.Add("other", new CircularDictionary {{"blah", null}});
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
JsonConvert.SerializeObject(circularDictionary, Formatting.Indented);
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Self referencing loop detected for type '" + classRef + "'.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(circularDictionary, Formatting.Indented);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -277,18 +289,18 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = @"Unexpected end when deserializing object. Line 2, position 9."
|
||||
#endif
|
||||
)]
|
||||
public void UnexpectedEnd()
|
||||
{
|
||||
string json = @"{
|
||||
""$id"":";
|
||||
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Unexpected end when deserializing object. Path '$id', line 2, position 9.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject<string[][]>(json,
|
||||
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
|
||||
});
|
||||
}
|
||||
|
||||
public class CircularReferenceClassConverter : JsonConverter
|
||||
|
||||
@@ -205,8 +205,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
#else
|
||||
Assert.AreEqual("[1] - 1 - The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.", errors[0]);
|
||||
#endif
|
||||
Assert.AreEqual("[2] - 2 - Unexpected token parsing date. Expected String, got StartArray.", errors[1]);
|
||||
Assert.AreEqual("[4] - 4 - Cannot convert null value to System.DateTime.", errors[2]);
|
||||
Assert.AreEqual("[2] - 2 - Unexpected token parsing date. Expected String, got StartArray. Path '[2]', line 4, position 10.", errors[1]);
|
||||
Assert.AreEqual("[4] - 4 - Cannot convert null value to System.DateTime. Path '[4]', line 8, position 13.", errors[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -294,10 +294,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
e = ex;
|
||||
}
|
||||
|
||||
Assert.AreEqual(@"Could not convert string to DateTime: kjhkjhkjhkjh. Line 1, position 16.", e.Message);
|
||||
Assert.AreEqual(@"Could not convert string to DateTime: kjhkjhkjhkjh. Path '[0][0]', line 1, position 16.", e.Message);
|
||||
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual(@"[0][0] - 0 - Could not convert string to DateTime: kjhkjhkjhkjh. Line 1, position 16.", errors[0]);
|
||||
Assert.AreEqual(@"[0][0] - 0 - Could not convert string to DateTime: kjhkjhkjhkjh. Path '[0][0]', line 1, position 16.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -314,8 +314,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
serializer.Deserialize(new JsonTextReader(new StringReader(json)), typeof (MyTypeWithRequiredMembers));
|
||||
|
||||
Assert.AreEqual(2, errors.Count);
|
||||
Assert.AreEqual(" - Required1 - Required property 'Required1' not found in JSON. Line 1, position 2.", errors[0]);
|
||||
Assert.AreEqual(" - Required2 - Required property 'Required2' not found in JSON. Line 1, position 2.", errors[1]);
|
||||
Assert.AreEqual(" - Required1 - Required property 'Required1' not found in JSON. Path '', line 1, position 2.", errors[0]);
|
||||
Assert.AreEqual(" - Required2 - Required property 'Required2' not found in JSON. Path '', line 1, position 2.", errors[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -335,8 +335,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
serializer.Deserialize(new JsonTextReader(new StringReader(json)), typeof(int[]));
|
||||
|
||||
Assert.AreEqual(2, errors.Count);
|
||||
Assert.AreEqual("[0] - 0 - Could not convert string to integer: a. Line 1, position 4.", errors[0]);
|
||||
Assert.AreEqual("[1] - 1 - Could not convert string to integer: b. Line 1, position 8.", errors[1]);
|
||||
Assert.AreEqual("[0] - 0 - Could not convert string to integer: a. Path '[0]', line 1, position 4.", errors[0]);
|
||||
Assert.AreEqual("[1] - 1 - Could not convert string to integer: b. Path '[1]', line 1, position 8.", errors[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -262,10 +262,10 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException))]
|
||||
public void DeserializeWithBadTypeName()
|
||||
{
|
||||
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
|
||||
string personRef = typeof(Person).AssemblyQualifiedName;
|
||||
|
||||
string json = @"{
|
||||
""$id"": ""1"",
|
||||
@@ -274,11 +274,16 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""Manager"": null
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject(json, typeof(Person), new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Type specified in JSON '" + employeeRef + @"' is not compatible with '" + personRef + @"'. Path '$type', line 3, position 143.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, typeof (Person), new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -302,11 +307,6 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException)
|
||||
#if !NETFX_CORE
|
||||
, ExpectedMessage = "Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Line 3, position 56."
|
||||
#endif
|
||||
)]
|
||||
public void DeserializeTypeNameOnly()
|
||||
{
|
||||
string json = @"{
|
||||
@@ -316,10 +316,15 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""Manager"": null
|
||||
}";
|
||||
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Type specified in JSON 'Newtonsoft.Json.Tests.TestObjects.Employee' was not resolved. Path '$type', line 3, position 56.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public interface ICorrelatedMessage
|
||||
|
||||
@@ -129,7 +129,7 @@ namespace Newtonsoft.Json.Tests
|
||||
catch (TException ex)
|
||||
{
|
||||
if (message != null)
|
||||
Assert.AreEqual(message, ex.Message);
|
||||
Assert.AreEqual(message, ex.Message, "Unexpected exception message." + Environment.NewLine + "Expected: " + message + Environment.NewLine + "Got: " + ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user