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

-g|--group option is unsafe for ][ in a string #55

Open
trentm opened this issue Jul 8, 2013 · 4 comments
Open

-g|--group option is unsafe for ][ in a string #55

trentm opened this issue Jul 8, 2013 · 4 comments
Assignees
Labels

Comments

@trentm
Copy link
Owner

trentm commented Jul 8, 2013

$ cat foo.json
["foo"]
["bar"]

$ json -f foo.json -g
[
  "foo",
  "bar"
]

$ cat boom.json
["foo"]
["boom]["]
["bar"]

$ json -f boom.json -g
json: error: input is not JSON: Syntax error at line 2, column 1:
        ["boom]["]
        ^
["foo"]
["boom]["]
["bar"]
@davepacheco
Copy link

I ran into a similar case that functions differently, which itself is surprising. I'm using v9.0.3.

@trentm's example from before works the same way as it did then:

$ cat foo.json 
["foo"]
["bar"]
$ json -f foo.json -g
[
  "foo",
  "bar"
]

and for reference:

$ json -f foo.json -ga
foo
bar

which seems surprising. But this is even more surprising:

$ cat bar.json 
[]
[]
$ json -f bar.json -g
json: error: "bar.json" is not JSON: Syntax error at line 2, column 1:
        []
        ^
[]
[]
$ json -f bar.json -ga
json: error: input is not JSON: Syntax error at line 2, column 1:
        []
        ^
[]
[]

@trentm
Copy link
Owner Author

trentm commented Mar 4, 2016

@davepacheco jsons grouping is not the best stuff in the world. The grouping of adjacent arrays (without streaming, i.e. json -g ...) is doing this content transformation:

        [/(\])\s*\n\s*(\[)/g].forEach(function (pat) {
            newBuffer = newBuffer.replace(pat, ',\n');
        });

Hence transforming:

[]
[]

to:

[,
]

which of course fails to parse. Either json does a more robust transformation here, or it could special case for empty arrays.

@trentm
Copy link
Owner Author

trentm commented Mar 4, 2016

@davepacheco Is this blocking/breaking you on something?

@davepacheco
Copy link

davepacheco commented Mar 4, 2016 via email

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

No branches or pull requests

3 participants
@trentm @davepacheco and others