-Added support for root object in JSONPath filters

This commit is contained in:
James Newton-King
2016-09-10 16:54:29 +12:00
parent 59618df631
commit ac2d619474
21 changed files with 128 additions and 44 deletions
@@ -949,5 +949,57 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
result = a.SelectTokens("$.[?(@.value)]").ToList();
Assert.AreEqual(4, result.Count);
}
[Test]
public void RootInFilter()
{
string json = @"[
{
""store"" : {
""book"" : [
{
""category"" : ""reference"",
""author"" : ""Nigel Rees"",
""title"" : ""Sayings of the Century"",
""price"" : 8.95
},
{
""category"" : ""fiction"",
""author"" : ""Evelyn Waugh"",
""title"" : ""Sword of Honour"",
""price"" : 12.99
},
{
""category"" : ""fiction"",
""author"" : ""Herman Melville"",
""title"" : ""Moby Dick"",
""isbn"" : ""0-553-21311-3"",
""price"" : 8.99
},
{
""category"" : ""fiction"",
""author"" : ""J. R. R. Tolkien"",
""title"" : ""The Lord of the Rings"",
""isbn"" : ""0-395-19395-8"",
""price"" : 22.99
}
],
""bicycle"" : {
""color"" : ""red"",
""price"" : 19.95
}
},
""expensive"" : 10
}
]";
JArray a = JArray.Parse(json);
List<JToken> result = a.SelectTokens("$.[?($.store.bicycle.price < 20)]").ToList();
Assert.AreEqual(1, result.Count);
result = a.SelectTokens("$.[?($.store.bicycle.price < 10)]").ToList();
Assert.AreEqual(0, result.Count);
}
}
}
@@ -419,6 +419,16 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
Assert.AreEqual(QueryOperator.Exists, orExpression.Expressions[1].Operator);
}
[Test]
public void FilterWithRoot()
{
JPath path = new JPath("[?($.name>=12.1)]");
BooleanQueryExpression expressions = (BooleanQueryExpression)((QueryFilter)path.Filters[0]).Expression;
Assert.AreEqual(2, expressions.Path.Count);
Assert.IsInstanceOf(typeof(RootFilter), expressions.Path[0]);
Assert.IsInstanceOf(typeof(FieldFilter), expressions.Path[1]);
}
[Test]
public void BadOr1()
{
@@ -86,7 +86,7 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{ "LastName", "LastName!" }
};
Assert.IsTrue(compositeExpression.IsMatch(o1));
Assert.IsTrue(compositeExpression.IsMatch(o1, o1));
JObject o2 = new JObject
{
@@ -94,14 +94,14 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{ "FirstName", "FirstName!" }
};
Assert.IsFalse(compositeExpression.IsMatch(o2));
Assert.IsFalse(compositeExpression.IsMatch(o2, o2));
JObject o3 = new JObject
{
{ "Title", "Title!" }
};
Assert.IsFalse(compositeExpression.IsMatch(o3));
Assert.IsFalse(compositeExpression.IsMatch(o3, o3));
}
[Test]
@@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{ "LastName", "LastName!" }
};
Assert.IsTrue(compositeExpression.IsMatch(o1));
Assert.IsTrue(compositeExpression.IsMatch(o1, o1));
JObject o2 = new JObject
{
@@ -152,14 +152,14 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
{ "FirstName", "FirstName!" }
};
Assert.IsTrue(compositeExpression.IsMatch(o2));
Assert.IsTrue(compositeExpression.IsMatch(o2, o2));
JObject o3 = new JObject
{
{ "Title", "Title!" }
};
Assert.IsFalse(compositeExpression.IsMatch(o3));
Assert.IsFalse(compositeExpression.IsMatch(o3, o3));
}
[Test]
@@ -175,10 +175,10 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
}
};
Assert.IsTrue(e1.IsMatch(new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e1.IsMatch(new JArray(2, 3, 4, 5)));
Assert.IsFalse(e1.IsMatch(new JArray(3, 4, 5)));
Assert.IsFalse(e1.IsMatch(new JArray(4, 5)));
Assert.IsTrue(e1.IsMatch(null, new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e1.IsMatch(null, new JArray(2, 3, 4, 5)));
Assert.IsFalse(e1.IsMatch(null, new JArray(3, 4, 5)));
Assert.IsFalse(e1.IsMatch(null, new JArray(4, 5)));
BooleanQueryExpression e2 = new BooleanQueryExpression
{
@@ -190,10 +190,10 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
}
};
Assert.IsTrue(e2.IsMatch(new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e2.IsMatch(new JArray(2, 3, 4, 5)));
Assert.IsTrue(e2.IsMatch(new JArray(3, 4, 5)));
Assert.IsFalse(e2.IsMatch(new JArray(4, 5)));
Assert.IsTrue(e2.IsMatch(null, new JArray(1, 2, 3, 4, 5)));
Assert.IsTrue(e2.IsMatch(null, new JArray(2, 3, 4, 5)));
Assert.IsTrue(e2.IsMatch(null, new JArray(3, 4, 5)));
Assert.IsFalse(e2.IsMatch(null, new JArray(4, 5)));
}
}
}