danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
726 stars 164 forks source link

Support `.length()` in jsonpath #374

Closed hongweipeng closed 2 years ago

hongweipeng commented 2 years ago

Currently, it supports $.length but not $.length() , aren't they the same.

danielaparker commented 2 years ago

The original Goessner JSONPath had @.length (but not $.length), and jsoncons jsonpath followed that (and added $.length). Basically, Goessner JSONPath used JavaScript for evaluating JSONPath expressions [?(expr)] and in JavaScript length is a property of an array, hence @.length. Some other implementations made that choice as well, as you can see in the JSONPath Comparisons, Script expression.

Jayway JSONPath, an influential Java implementation, introduced functions that can be invoked at the tail end of a path, e.g. $.sum() to return the sum of items in array $, and @.length() to return the number of items in array @. Very few other implementations followed that, though, e.g. in the JSONPath Comparisons, in Function sum, only two other implementations support $.sum().

jsoncons jsonpath added support for functions with parameters, e.g. sum($) and length(@). Other implementations that use JavaScript, Python or PHP to evaluate expressions in filters also have some support for functions with parameters, but these are JavaScript, Python or PHP functions, e.g. in original Goessner JSONPath, $[?(Math.abs(@) == 10)] works. But there is no standard across implementations.

So we won't add $.length(), as apart from Jayway JSONPath, the notion of functions that can be invoked at the tail end of a path hasn't caught on with other implementations.