Merge branch 'feature/fix-root-filter' of https://github.com/asiffermann/Newtonsoft.Json into asiffermann-feature/fix-root-filter
This commit is contained in:
@@ -1271,11 +1271,85 @@ namespace Newtonsoft.Json.Tests.Linq.JsonPath
|
|||||||
|
|
||||||
JArray a = JArray.Parse(json);
|
JArray a = JArray.Parse(json);
|
||||||
|
|
||||||
List<JToken> result = a.SelectTokens("$.[?($.store.bicycle.price < 20)]").ToList();
|
List<JToken> result = a.SelectTokens("$.[?($.[0].store.bicycle.price < 20)]").ToList();
|
||||||
Assert.AreEqual(1, result.Count);
|
Assert.AreEqual(1, result.Count);
|
||||||
|
|
||||||
result = a.SelectTokens("$.[?($.store.bicycle.price < 10)]").ToList();
|
result = a.SelectTokens("$.[?($.[0].store.bicycle.price < 10)]").ToList();
|
||||||
Assert.AreEqual(0, result.Count);
|
Assert.AreEqual(0, result.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RootInFilterWithRootObject()
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}";
|
||||||
|
|
||||||
|
JObject a = JObject.Parse(json);
|
||||||
|
|
||||||
|
List<JToken> result = a.SelectTokens("$..book[?(@.price <= $['expensive'])]").ToList();
|
||||||
|
Assert.AreEqual(2, result.Count);
|
||||||
|
|
||||||
|
result = a.SelectTokens("$.store..[?(@.price > $.expensive)]").ToList();
|
||||||
|
Assert.AreEqual(3, result.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RootInFilterWithInitializers()
|
||||||
|
{
|
||||||
|
JObject rootObject = new JObject
|
||||||
|
{
|
||||||
|
{ "referenceDate", new JValue(DateTime.MinValue) },
|
||||||
|
{
|
||||||
|
"dateObjectsArray",
|
||||||
|
new JArray()
|
||||||
|
{
|
||||||
|
new JObject { { "date", new JValue(DateTime.MinValue) } },
|
||||||
|
new JObject { { "date", new JValue(DateTime.MaxValue) } },
|
||||||
|
new JObject { { "date", new JValue(DateTime.Now) } },
|
||||||
|
new JObject { { "date", new JValue(DateTime.MinValue) } },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<JToken> result = rootObject.SelectTokens("$.dateObjectsArray[?(@.date == $.referenceDate)]").ToList();
|
||||||
|
Assert.AreEqual(2, result.Count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
|||||||
|
|
||||||
public override IEnumerable<JToken> ExecuteFilter(JToken root, IEnumerable<JToken> current, bool errorWhenNoMatch)
|
public override IEnumerable<JToken> ExecuteFilter(JToken root, IEnumerable<JToken> current, bool errorWhenNoMatch)
|
||||||
{
|
{
|
||||||
return root;
|
yield return root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user