JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.82k stars 3.26k forks source link

To expose Linq.JsonPath.JPath to enable query-centric use cases #2985

Open xhoms opened 1 month ago

xhoms commented 1 month ago

It would be great to have the JPath class and a slightly modified version of its Evaluate(JToken obj, JsonSelectSettings? settings) method publicly accessible to be able to use pre-parsed versions of JSON Paths in query intensive applications like in this mocked code that attempts to check if a JObject instance matches any of multiple rules in a large collection.

List<string> veryLargeCollectionOfJPathRules = [
     "$..childN.propA",
     "$..childN.childM.propA",
     ];

var compiledRules = veryLargeCollectionOfJPathRules.ConvertAll(path => new JPath(path)); // to have JPath exposed
var hasMatch = compiledRules.Any(rule => rule.Evaluate(objectUnderTest).Any()); // to have Evaluate() exposed
JJLovesLife commented 2 days ago

Vote for this. If we query the same JSONPath on tons of JTokens, parsing the same JSONPath every time is a huge performance burden.