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

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

Comments

Projects
None yet
2 participants
@trentm
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

This comment has been minimized.

Show comment
Hide comment
@davepacheco

davepacheco Mar 4, 2016

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:
        []
        ^
[]
[]

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

This comment has been minimized.

Show comment
Hide comment
@trentm

trentm Mar 4, 2016

Owner

@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.

Owner

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

This comment has been minimized.

Show comment
Hide comment
@trentm

trentm Mar 4, 2016

Owner

@davepacheco Is this blocking/breaking you on something?

Owner

trentm commented Mar 4, 2016

@davepacheco Is this blocking/breaking you on something?

@davepacheco

This comment has been minimized.

Show comment
Hide comment
@davepacheco

davepacheco Mar 4, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment