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

Please do not change output strings #43

Closed
kseistrup opened this issue Apr 19, 2018 · 10 comments
Closed

Please do not change output strings #43

kseistrup opened this issue Apr 19, 2018 · 10 comments
Labels

Comments

@kseistrup
Copy link
Contributor

Please consider the following JSON:

{
  "dependencies": {
    "ssb-client": "http://localhost:8989/blobs/get/&EAaUpI+wrJM5/ly1RqZW0GAEF4PmCAmABBj7e6UIrL0=.sha256",
    "ssb-mentions": "http://localhost:8989/blobs/get/&GjuxknqKwJqHznKueFNCyIh52v1woz5PB41vqmoHfyM=.sha256"
  }
}

Seemingly, I can gron and ungron this to achieve an identical output:

$ gron < test.json
json = {};
json.dependencies = {};
json.dependencies["ssb-client"] = "http://localhost:8989/blobs/get/&EAaUpI+wrJM5/ly1RqZW0GAEF4PmCAmABBj7e6UIrL0=.sha256";
json.dependencies["ssb-mentions"] = "http://localhost:8989/blobs/get/&GjuxknqKwJqHznKueFNCyIh52v1woz5PB41vqmoHfyM=.sha256";
$ gron < test.json | ungron
{
  "dependencies": {
    "ssb-client": "http://localhost:8989/blobs/get/&EAaUpI+wrJM5/ly1RqZW0GAEF4PmCAmABBj7e6UIrL0=.sha256",
    "ssb-mentions": "http://localhost:8989/blobs/get/&GjuxknqKwJqHznKueFNCyIh52v1woz5PB41vqmoHfyM=.sha256"
  }
}

However, if stdout is not a TTY, what you get is something entirely different:

$ gron < test.json | ungron | cat   # same thing happens if you redirect to a file
{
  "dependencies": {
    "ssb-client": "http://localhost:8989/blobs/get/\u0026EAaUpI+wrJM5/ly1RqZW0GAEF4PmCAmABBj7e6UIrL0=.sha256",
    "ssb-mentions": "http://localhost:8989/blobs/get/\u0026GjuxknqKwJqHznKueFNCyIh52v1woz5PB41vqmoHfyM=.sha256"
  }
}

IMHO, gron should make identical output no matter what. Ideally, gron shouldn't try to outsmart the user and start interpreting string values or displaying them differently (even if the meaning is the same). At least provide a commandline switch so that the original strings are preserved.

Cheers.

@tomnomnom tomnomnom added the bug label Apr 19, 2018
@tomnomnom
Copy link
Owner

Hi @kseistrup! Thanks for raising an issue!

It looks like this is actually the default behaviour of Go's encoding/json package (which is used only when the output is not colourised). I'll see about fixing this for you now

@tomnomnom
Copy link
Owner

@kseistrup this should be fixed in master now. Are you able to update with go get -u github.com/tomnomnom/gron, or would you like me to upload a binary so you can test?

@kseistrup
Copy link
Contributor Author

I can go get, so I'll try it right away…

@kseistrup
Copy link
Contributor Author

This seems to have fixed the issue. Thanks for fixing it so swiftly!

@kseistrup
Copy link
Contributor Author

While the JSON itself is identical, the new code adds a blank line after the JSON whenever stdout is not a TTY:

$ gron config.json | grep friends
json.friends = {};
json.friends.hops = 3;
$ gron config.json | grep friends | ungron
{
  "friends": {
    "hops": 3
  }
}
$ gron config.json | grep friends | ungron | cat
{
  "friends": {
    "hops": 3
  }
}

$ 

@kseistrup kseistrup reopened this Apr 24, 2018
@rjmunro
Copy link

rjmunro commented Jul 5, 2018

In case like me you were wondering what is different about the 2 examples, here is something minimal to illustrate the issue:

$ echo 'json[0] = "&";' | ungron
[
   "&"
]
$ echo 'json[0] = "&";' | ungron | cat
[
  "\u0026"
]

@kseistrup
Copy link
Contributor Author

Thanks for the minimal example, @rjmunro.

That issue has been solved, but the JSON outputs are still not identical:

$ echo 'json[0]="&";' | ungron
[
  "&"
]
$ echo 'json[0]="&";' | ungron | cat
[
  "&"
]

$ 

@tomnomnom
Copy link
Owner

Hey; thanks for reminding me about this!

I'll see if I can get a fix in for it soon. Happy to accept PRs for it too ;)

@tomnomnom
Copy link
Owner

tomnomnom commented Jul 5, 2018

I managed to get a few minutes free to sort this.

The changes are available in v0.6.0.

Please let me know if you hit any more problems!

@kseistrup
Copy link
Contributor Author

@tomnomnom,

For lists and single elements the indentation is different:

$ echo 3 | gron | ungron
 3
$ echo 3 | gron | ungron | cat
3
$ echo '[1,2,3]' | gron | ungron
[
   1,
   2,
   3
]
$ echo '[1,2,3]' | gron | ungron | cat
[
  1,
  2,
  3
]

Mappings are not affected, though:

$ echo '{"a":true,"b":false}' | gron | ungron
{
  "a": true,
  "b": false
}
$ echo '{"a":true,"b":false}' | gron | ungron | cat
{
  "a": true,
  "b": false
}
$ 

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