Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new-lines & tabs to whitespace #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jsen-
Copy link

@jsen- jsen- commented Jun 21, 2022

this PR allows newlines and tabs to be used everywhere space is allowed

@MeirShpilraien
Copy link
Collaborator

this PR allows newlines and tabs to be used everywhere space is allowed

I am not sure this is a valid json path, I guess tabs is OK, but new lines? @oshadmi what do you think?

@oshadmi
Copy link

oshadmi commented Aug 25, 2022

this PR allows newlines and tabs to be used everywhere space is allowed

I am not sure this is a valid json path, I guess tabs is OK, but new lines? @oshadmi what do you think?

Do you know the performance cost of being less strict and allowing white-spaces?

I think that within a filter expression, or a function expression, it could be handy to allow white-spaces, even new-lines, and if we allow white-spaces in those, then the overall look of a path might not be easy/pretty to ready, so we should also allow indenting the entire JSONPath expression, e.g.,

$..book[?(
         (@.category == "reference" ||
          @.price > 12)
       &&
         @.author == "J. R. R. Tolkien"
)]

Anyway, it makes more sense to me to either allow all white-spaces everywhere, or not allow at all - much more intuitive and easier to explain.

I tried the above JSONPath with Jayway JsonPath Evaluator and it works fine.

But it seems even spaces are not supported by some underlying libraries (JSONPath may not be regarded as a full language, where you have a lexer that consumes white-spaces, as in more complex languages, but rather more like reg exp)

We can also consider to not allow white-spaces and let the application/python client, other clients, etc., to actually strip the white-spaces if they prefer to be less strict.

Tried this and even space/tab are either failing to return a valid result, or throw a Lexical error exception:

const {JSONPath} = require('jsonpath-plus');
const jsonpath = require('jsonpath');

const json = { "a": 10, "b": [0, 1, 2, 3, 4, 5]}

const good_paths = ["$.a", "$.b[1:3]", "$"];
const bad_paths = ["$. a", "$.b[ 1:3]", "   $"];

good_paths.forEach(test_jsonpath);
bad_paths.forEach(test_jsonpath);

function test_jsonpath(p) {
    try {
        console.log("jsonpath-plus '" + p + "' = ")
        console.log(JSONPath({path: p, json: json}));
    } catch (e) {
        console.log(e);
    }

    try {
        console.log("jsonpath '" + p + "' = ");
        console.log(jsonpath.query(json, p));
    } catch (e) {
        console.log(e);
    }
    console.log("");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants