Throw for CheckAdditionalContent when extra content is preceded by comments. (#1183)

Fixes #1182
This commit is contained in:
Jon Hanna
2017-01-16 14:48:52 +13:00
committed by James Newton-King
parent 7e4b3448c7
commit 9727996b1a
2 changed files with 52 additions and 2 deletions
@@ -8153,6 +8153,53 @@ Path '', line 1, position 1.");
ExceptionAssert.Throws<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 1, position 7.");
}
[Test]
public void CheckAdditionalContentJustComment()
{
string json = "{one:1} // This is just a comment";
JsonSerializerSettings settings = new JsonSerializerSettings {CheckAdditionalContent = true};
JsonSerializer s = JsonSerializer.Create(settings);
IDictionary<string, int> o = s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json)));
Assert.IsNotNull(o);
Assert.AreEqual(1, o["one"]);
}
[Test]
public void CheckAdditionalContentJustMultipleComments()
{
string json = @"{one:1} // This is just a comment
/* This is just a comment
over multiple
lines.*/
// This is just another comment.";
JsonSerializerSettings settings = new JsonSerializerSettings {CheckAdditionalContent = true};
JsonSerializer s = JsonSerializer.Create(settings);
IDictionary<string, int> o = s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json)));
Assert.IsNotNull(o);
Assert.AreEqual(1, o["one"]);
}
[Test]
public void CheckAdditionalContentCommentsThenAnotherObject()
{
string json = @"{one:1} // This is just a comment
/* This is just a comment
over multiple
lines.*/
// This is just another comment. But here comes an empty object.
{}";
JsonSerializerSettings settings = new JsonSerializerSettings { CheckAdditionalContent = true };
JsonSerializer s = JsonSerializer.Create(settings);
ExceptionAssert.Throws<JsonReaderException>(() => { s.Deserialize<Dictionary<string, int>>(new JsonTextReader(new StringReader(json))); }, "Additional text encountered after finished reading JSON content: {. Path '', line 7, position 0.");
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
[Test]
public void DeserializeException()
@@ -169,9 +169,12 @@ namespace Newtonsoft.Json.Serialization
if (checkAdditionalContent)
{
if (reader.Read() && reader.TokenType != JsonToken.Comment)
while (reader.Read())
{
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
if (reader.TokenType != JsonToken.Comment)
{
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
}
}
}