diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 7655dd52..25915659 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -8153,6 +8153,53 @@ Path '', line 1, position 1."); ExceptionAssert.Throws(() => { s.Deserialize>(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 o = s.Deserialize>(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 o = s.Deserialize>(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(() => { s.Deserialize>(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() diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index ce4b123b..5ff29e78 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -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."); + } } }