-Fixed jsonpath error when parsing a property that follows an escaped property

-Changed reader/writer/token path to escape property names containing special characters
This commit is contained in:
James Newton-King
2014-10-18 15:36:42 +13:00
parent 01f1c5e261
commit 88a29704da
12 changed files with 385 additions and 287 deletions
@@ -643,5 +643,18 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{
ExceptionAssert.Throws<JsonException>(() => { new JPath("[1]Blah"); }, "Unexpected character following indexer: B");
}
[Test]
public void PropertyFollowingEscapedPropertyName()
{
JPath path = new JPath("frameworks.aspnetcore50.dependencies.['System.Xml.ReaderWriter'].source");
Assert.AreEqual(5, path.Filters.Count);
Assert.AreEqual("frameworks", ((FieldFilter)path.Filters[0]).Name);
Assert.AreEqual("aspnetcore50", ((FieldFilter)path.Filters[1]).Name);
Assert.AreEqual("dependencies", ((FieldFilter)path.Filters[2]).Name);
Assert.AreEqual("System.Xml.ReaderWriter", ((FieldFilter)path.Filters[3]).Name);
Assert.AreEqual("source", ((FieldFilter)path.Filters[4]).Name);
}
}
}
@@ -184,6 +184,55 @@ undefined
Assert.AreEqual("person.$id", idProperty.Path);
}
[Test]
public void EscapedPath()
{
string json = @"{
""frameworks"": {
""aspnetcore50"": {
""dependencies"": {
""System.Xml.ReaderWriter"": {
""source"": ""NuGet""
}
}
}
}
}";
JObject o = JObject.Parse(json);
JToken v1 = o["frameworks"]["aspnetcore50"]["dependencies"]["System.Xml.ReaderWriter"]["source"];
Console.WriteLine(v1.Path);
JToken v2 = o.SelectToken(v1.Path);
Assert.AreEqual(v1, v2);
}
[Test]
public void EscapedPathTests()
{
EscapedPathAssert("this has spaces", "['this has spaces']");
EscapedPathAssert("(RoundBraces)", "['(RoundBraces)']");
EscapedPathAssert("[SquareBraces]", "['[SquareBraces]']");
EscapedPathAssert("this.has.dots", "['this.has.dots']");
}
private void EscapedPathAssert(string propertyName, string expectedPath)
{
int v1 = int.MaxValue;
JValue value = new JValue(v1);
JObject o = new JObject(new JProperty(propertyName, value));
Assert.AreEqual(expectedPath, value.Path);
JValue selectedValue = (JValue)o.SelectToken(value.Path);
Assert.AreEqual(value, selectedValue);
}
[Test]
public void ForEach()
{