-Fixed JsonConvert.PopulateObject error with starting comment

This commit is contained in:
James Newton-King
2015-09-30 15:38:33 +13:00
parent 2eed44a2f8
commit b30e1a178b
2 changed files with 65 additions and 2 deletions
@@ -60,6 +60,64 @@ namespace Newtonsoft.Json.Tests
[TestFixture]
public class JsonConvertTest : TestFixtureBase
{
public class PopulateTestObject
{
public decimal Prop { get; set; }
}
[Test]
public void PopulateObjectWithHeaderComment()
{
string json = @"// file header
{
""prop"": 1.0
}";
PopulateTestObject o = new PopulateTestObject();
JsonConvert.PopulateObject(json, o);
Assert.AreEqual(1m, o.Prop);
}
[Test]
public void PopulateObjectWithMultipleHeaderComment()
{
string json = @"// file header
// another file header?
{
""prop"": 1.0
}";
PopulateTestObject o = new PopulateTestObject();
JsonConvert.PopulateObject(json, o);
Assert.AreEqual(1m, o.Prop);
}
[Test]
public void PopulateObjectWithNoContent()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
string json = @"";
PopulateTestObject o = new PopulateTestObject();
JsonConvert.PopulateObject(json, o);
}, "No JSON content found. Path '', line 0, position 0.");
}
[Test]
public void PopulateObjectWithOnlyComment()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
string json = @"// file header";
PopulateTestObject o = new PopulateTestObject();
JsonConvert.PopulateObject(json, o);
}, "No JSON content found. Path '', line 1, position 14.");
}
[Test]
public void DefaultSettings()
{
@@ -70,8 +70,13 @@ namespace Newtonsoft.Json.Serialization
JsonContract contract = Serializer._contractResolver.ResolveContract(objectType);
if (reader.TokenType == JsonToken.None)
reader.Read();
while (reader.TokenType == JsonToken.None || reader.TokenType == JsonToken.Comment)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "No JSON content found.");
}
}
if (reader.TokenType == JsonToken.StartArray)
{