-Fixed bug when deserializing JObject with comments
This commit is contained in:
@@ -253,6 +253,37 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(input.Result, deserialized.Result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeJObjectWithComments()
|
||||
{
|
||||
JObject o = (JObject)JsonConvert.DeserializeObject(@"{/*Test*/""A"":/* Test */true/* Test */,/* Test */""B"":/* Test */false/* Test */}");
|
||||
Assert.AreEqual(2, o.Count);
|
||||
Assert.AreEqual(true, (bool)o["A"]);
|
||||
Assert.AreEqual(false, (bool)o["B"]);
|
||||
|
||||
o = (JObject)JsonConvert.DeserializeObject(@"{/* Test */}");
|
||||
Assert.AreEqual(0, o.Count);
|
||||
|
||||
o = (JObject)JsonConvert.DeserializeObject(@"{""A"": true/* Test */}");
|
||||
Assert.AreEqual(1, o.Count);
|
||||
Assert.AreEqual(true, (bool)o["A"]);
|
||||
}
|
||||
|
||||
public class CommentTestObject
|
||||
{
|
||||
public bool? A { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeCommentTestObjectWithComments()
|
||||
{
|
||||
CommentTestObject o = JsonConvert.DeserializeObject<CommentTestObject>(@"{/* Test */}");
|
||||
Assert.AreEqual(null, o.A);
|
||||
|
||||
o = JsonConvert.DeserializeObject<CommentTestObject>(@"{""A"": true/* Test */}");
|
||||
Assert.AreEqual(true, o.A);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonSerializerProperties()
|
||||
{
|
||||
|
||||
@@ -39,8 +39,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
internal Required? _required;
|
||||
internal bool _hasExplicitDefaultValue;
|
||||
internal object _defaultValue;
|
||||
|
||||
private object _defaultValue;
|
||||
private bool _hasGeneratedDefaultValue;
|
||||
private string _propertyName;
|
||||
private bool _skipPropertyNameEscape;
|
||||
|
||||
@@ -236,8 +236,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
string propertyName = (string)reader.Value;
|
||||
if (!reader.Read())
|
||||
break;
|
||||
do
|
||||
{
|
||||
if (!reader.Read())
|
||||
break;
|
||||
} while (reader.TokenType == JsonToken.Comment);
|
||||
|
||||
if (CheckPropertyName(reader, propertyName))
|
||||
continue;
|
||||
@@ -245,6 +248,10 @@ namespace Newtonsoft.Json.Serialization
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WriteToken(reader, true, true);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.Comment)
|
||||
{
|
||||
// eat
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteEndObject();
|
||||
|
||||
Reference in New Issue
Block a user