-Fixed JSONPath recursive descent queries

This commit is contained in:
James Newton-King
2017-04-26 11:09:15 +12:00
parent c80be730f8
commit b3d514b617
7 changed files with 232 additions and 46 deletions
@@ -51,6 +51,123 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
[TestFixture]
public class JPathExecuteTests : TestFixtureBase
{
[Test]
public void ScanFilter()
{
string json = @"{
""elements"": [
{
""id"": ""A"",
""children"": [
{
""id"": ""AA"",
""children"": [
{
""id"": ""AAA""
},
{
""id"": ""AAB""
}
]
},
{
""id"": ""AB""
}
]
},
{
""id"": ""B"",
""children"": []
}
]
}";
JObject models = JObject.Parse(json);
var results = models.SelectTokens("$.elements..[?(@.id=='AAA')]").ToList();
Assert.AreEqual(1, results.Count);
Assert.AreEqual(models["elements"][0]["children"][0]["children"][0], results[0]);
}
[Test]
public void FilterTrue()
{
string json = @"{
""elements"": [
{
""id"": ""A"",
""children"": [
{
""id"": ""AA"",
""children"": [
{
""id"": ""AAA""
},
{
""id"": ""AAB""
}
]
},
{
""id"": ""AB""
}
]
},
{
""id"": ""B"",
""children"": []
}
]
}";
JObject models = JObject.Parse(json);
var results = models.SelectTokens("$.elements[?(true)]").ToList();
Assert.AreEqual(2, results.Count);
Assert.AreEqual(results[0], models["elements"][0]);
Assert.AreEqual(results[1], models["elements"][1]);
}
[Test]
public void ScanFilterTrue()
{
string json = @"{
""elements"": [
{
""id"": ""A"",
""children"": [
{
""id"": ""AA"",
""children"": [
{
""id"": ""AAA""
},
{
""id"": ""AAB""
}
]
},
{
""id"": ""AB""
}
]
},
{
""id"": ""B"",
""children"": []
}
]
}";
JObject models = JObject.Parse(json);
var results = models.SelectTokens("$.elements..[?(true)]").ToList();
Assert.AreEqual(25, results.Count);
}
[Test]
public void ScanQuoted()
{
@@ -202,6 +202,29 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
Assert.AreEqual("Blah", ((ScanFilter)path.Filters[0]).Name);
}
[Test]
public void QueryTrue()
{
JPath path = new JPath("$.elements[?(true)]");
Assert.AreEqual(2, path.Filters.Count);
Assert.AreEqual("elements", ((FieldFilter)path.Filters[0]).Name);
Assert.AreEqual(QueryOperator.Exists, ((QueryFilter)path.Filters[1]).Expression.Operator);
}
[Test]
public void ScanQuery()
{
JPath path = new JPath("$.elements..[?(@.id=='AAA')]");
Assert.AreEqual(2, path.Filters.Count);
Assert.AreEqual("elements", ((FieldFilter)path.Filters[0]).Name);
BooleanQueryExpression expression = (BooleanQueryExpression)((QueryScanFilter) path.Filters[1]).Expression;
List<PathFilter> paths = (List<PathFilter>)expression.Left;
Assert.IsInstanceOf(typeof(FieldFilter), paths[0]);
}
[Test]
public void WildcardScanWithRoot()
{