-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);
|
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]
|
[Test]
|
||||||
public void JsonSerializerProperties()
|
public void JsonSerializerProperties()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
internal Required? _required;
|
internal Required? _required;
|
||||||
internal bool _hasExplicitDefaultValue;
|
internal bool _hasExplicitDefaultValue;
|
||||||
internal object _defaultValue;
|
|
||||||
|
|
||||||
|
private object _defaultValue;
|
||||||
private bool _hasGeneratedDefaultValue;
|
private bool _hasGeneratedDefaultValue;
|
||||||
private string _propertyName;
|
private string _propertyName;
|
||||||
private bool _skipPropertyNameEscape;
|
private bool _skipPropertyNameEscape;
|
||||||
|
|||||||
@@ -236,8 +236,11 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
if (reader.TokenType == JsonToken.PropertyName)
|
if (reader.TokenType == JsonToken.PropertyName)
|
||||||
{
|
{
|
||||||
string propertyName = (string)reader.Value;
|
string propertyName = (string)reader.Value;
|
||||||
if (!reader.Read())
|
do
|
||||||
break;
|
{
|
||||||
|
if (!reader.Read())
|
||||||
|
break;
|
||||||
|
} while (reader.TokenType == JsonToken.Comment);
|
||||||
|
|
||||||
if (CheckPropertyName(reader, propertyName))
|
if (CheckPropertyName(reader, propertyName))
|
||||||
continue;
|
continue;
|
||||||
@@ -245,6 +248,10 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
writer.WritePropertyName(propertyName);
|
writer.WritePropertyName(propertyName);
|
||||||
writer.WriteToken(reader, true, true);
|
writer.WriteToken(reader, true, true);
|
||||||
}
|
}
|
||||||
|
else if (reader.TokenType == JsonToken.Comment)
|
||||||
|
{
|
||||||
|
// eat
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
writer.WriteEndObject();
|
writer.WriteEndObject();
|
||||||
|
|||||||
Reference in New Issue
Block a user