-Added JsonLoadSettings

-Added CommentsHandling
-Changed LINQ to JSON load to ignore array comments by default
This commit is contained in:
James Newton-King
2015-10-06 22:17:25 +13:00
parent 172c1d8f30
commit 2bf31b9605
22 changed files with 366 additions and 68 deletions
@@ -544,5 +544,21 @@ Parameter name: index");
l = decks.ToList();
Assert.AreEqual(1, l.Count);
}
[Test]
public void Parse_NoComments()
{
string json = "[1,2/*comment*/,3]";
JArray a = JArray.Parse(json, new JsonLoadSettings
{
CommentHandling = CommentHandling.Ignore
});
Assert.AreEqual(3, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(3, (int)a[2]);
}
}
}
@@ -1977,5 +1977,21 @@ Parameter name: arrayIndex");
Assert.AreEqual(@"{""foo"":""bar"",""name"":""Daniel Wertheim"",""birthDate"":""0001-01-01T00:00:00"",""lastModified"":""0001-01-01T00:00:00""}", json);
}
[Test]
public void Parse_NoComments()
{
string json = "{'prop':[1,2/*comment*/,3]}";
JObject o = JObject.Parse(json, new JsonLoadSettings
{
CommentHandling = CommentHandling.Ignore
});
Assert.AreEqual(3, o["prop"].Count());
Assert.AreEqual(1, (int)o["prop"][0]);
Assert.AreEqual(2, (int)o["prop"][1]);
Assert.AreEqual(3, (int)o["prop"][2]);
}
}
}
@@ -1240,5 +1240,21 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual("[0]", a[0].Path);
}
[Test]
public void Parse_NoComments()
{
string json = "{'prop':[1,2/*comment*/,3]}";
JToken o = JToken.Parse(json, new JsonLoadSettings
{
CommentHandling = CommentHandling.Ignore
});
Assert.AreEqual(3, o["prop"].Count());
Assert.AreEqual(1, (int)o["prop"][0]);
Assert.AreEqual(2, (int)o["prop"][1]);
Assert.AreEqual(3, (int)o["prop"][2]);
}
}
}
@@ -171,13 +171,34 @@ namespace Newtonsoft.Json.Tests.Linq
]");
JsonTextReader jsonReader = new JsonTextReader(textReader);
JArray a = (JArray)JToken.ReadFrom(jsonReader);
JArray a = (JArray)JToken.ReadFrom(jsonReader, new JsonLoadSettings
{
CommentHandling = CommentHandling.Load
});
Assert.AreEqual(4, a.Count);
Assert.AreEqual(JTokenType.Comment, a[0].Type);
Assert.AreEqual(" hi", ((JValue)a[0]).Value);
}
[Test]
public void CommentsAndReadFrom_IgnoreComments()
{
StringReader textReader = new StringReader(@"[
// hi
1,
2,
3
]");
JsonTextReader jsonReader = new JsonTextReader(textReader);
JArray a = (JArray)JToken.ReadFrom(jsonReader);
Assert.AreEqual(3, a.Count);
Assert.AreEqual(JTokenType.Integer, a[0].Type);
Assert.AreEqual(1, ((JValue)a[0]).Value);
}
[Test]
public void StartingCommentAndReadFrom()
{
@@ -190,7 +211,10 @@ namespace Newtonsoft.Json.Tests.Linq
]");
JsonTextReader jsonReader = new JsonTextReader(textReader);
JValue v = (JValue)JToken.ReadFrom(jsonReader);
JValue v = (JValue)JToken.ReadFrom(jsonReader, new JsonLoadSettings
{
CommentHandling = CommentHandling.Load
});
Assert.AreEqual(JTokenType.Comment, v.Type);
@@ -200,6 +224,31 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(0, lineInfo.LinePosition);
}
[Test]
public void StartingCommentAndReadFrom_IgnoreComments()
{
StringReader textReader = new StringReader(@"
// hi
[
1,
2,
3
]");
JsonTextReader jsonReader = new JsonTextReader(textReader);
JArray a = (JArray)JToken.ReadFrom(jsonReader, new JsonLoadSettings
{
CommentHandling = CommentHandling.Ignore
});
Assert.AreEqual(JTokenType.Array, a.Type);
IJsonLineInfo lineInfo = a;
Assert.AreEqual(true, lineInfo.HasLineInfo());
Assert.AreEqual(3, lineInfo.LineNumber);
Assert.AreEqual(1, lineInfo.LinePosition);
}
[Test]
public void StartingUndefinedAndReadFrom()
{
@@ -881,10 +881,9 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(3, o.Count);
Assert.AreEqual(true, (bool)o["A"]);
Assert.AreEqual(false, (bool)o["B"]);
Assert.AreEqual(3, o["C"].Count());
Assert.AreEqual(JTokenType.Comment, o["C"][0].Type);
Assert.AreEqual(1, (int)o["C"][1]);
Assert.AreEqual(JTokenType.Comment, o["C"][2].Type);
Assert.AreEqual(1, o["C"].Count());
Assert.AreEqual(1, (int)o["C"][0]);
Assert.IsTrue(JToken.DeepEquals(o, JObject.Parse(json)));
json = @"{/* Test */}";