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 a flag to output line numbers of location of objects in actual JSON #33

Closed
bbbco opened this issue Apr 3, 2018 · 3 comments
Closed

Comments

@bbbco
Copy link

bbbco commented Apr 3, 2018

I'd like to be able to output the line numbers of the location of objects in the actual JSON. This would be helpful in utilizing gron to make JSON files searchable via reference in command line text editors like VIM (see https://vi.stackexchange.com/questions/15682/is-there-a-vim-plugin-available-to-add-jsonpath-jq-jmespath-path-searching ).

I imagine it could output the line numbers inside a javascript comment so as to ensure that the output remains javascript compatible.

json = []; // line 1
json[0] = {}; // line 2
json[0].commit = {}; // line 3
json[0].commit.author = {}; // line 4
json[0].commit.author.date = "2016-07-02T10:51:21Z"; // line 5
json[0].commit.author.email = "mail@tomnomnom.com"; // line 6
json[0].commit.author.name = "Tom Hudson"; // line 7
...
@tomnomnom
Copy link
Owner

Hi @bbbco! Thank you for raising an issue :)

I like the idea, but I'm not entirely sure how easy it would be to implement. As far as I know, Go's JSON package doesn't deal with 'lines' when decoding JSON - even internally; it only maintains a byte offset as new-line characters don't have any meaning in JSON.

Short of writing my own JSON parser (or copying the parser form the standard library) and tracking the number of new-line characters seen I can't think of a way to do this. Even the byte-offset is only exposed when a parse error is encountered (e.g. when a json.UnmarshalTypeError type is returned).

I'm open to suggestions for good ways to do this if you or anyone else has any!

@bbbco
Copy link
Author

bbbco commented Apr 3, 2018

Ah yep, I'm sorry, I didn't look at your code. I didn't realize you don't have your own custom JSON parser.

If that's not in the plans, I'll see about looking elsewhere.

Thanks for the great response though!

@tomnomnom
Copy link
Owner

@bbbco no worries! Sorry I couldn't be more help!

I'll close this for now, but I ever think of a way to do it I'll let you know

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

No branches or pull requests

2 participants