ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

Accept hyphens in jsonpath #92

Closed nervo closed 2 years ago

nervo commented 2 years ago

This pr adds hyphens ("-") in the map of allowed characters in jsonpath.

According to the provided test, some jsonpath such as $.foo-bar are now possible :)

ohler55 commented 2 years ago

The reason - was not included is that it is a problem parsing filters like foo[?(@.a-1)] versus foo[?(@.a - 1)]. If I can verify that there is a way to assure there is no issue there then I'll merge. You can also use the bracket notation to have a - in a path name.

ohler55 commented 2 years ago

Here is a concrete example of the issue with allowing "-" or other math operators. Try $[?(@.x - 1 == @.y)] on [{"x":1,"y":0}]. It should match. Now take the tight way that many people use, $[?(@.x-1 == @.y)]. Is that meant to be a key of "x-1" or x - 1? The results are ambiguous. That's the reason -, +, *, and other common math operators are not parsed as path elements. You can use the bracket notation for such as $[?(@["x-1"] == @y)] on [{"x-1":1,"y":1}] to get a match though.

ohler55 commented 2 years ago

@nervo care to discuss or come up with alternatives?

ohler55 commented 2 years ago

Unless you can figure out an efficient way to determine the intent of the user to disambiguate the use of hyphens in a path versus a minus sign in and equation I have to close this PR. I suspect there is a way to do so by looking at the operators in the script but it's not something I'm will to tackle yet.