-Fixed JsonConvert.PopulateObject error with starting comment
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user