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

JSON #70

Open
equivalent opened this issue Mar 12, 2021 · 8 comments · May be fixed by #83
Open

JSON #70

equivalent opened this issue Mar 12, 2021 · 8 comments · May be fixed by #83
Assignees
Labels
enhancement New feature or request

Comments

@equivalent
Copy link

equivalent commented Mar 12, 2021

love the gem, but is there a way how to print JSON format ? Meaning hash rocket => replaced by : , keys in quotes, ...) ?

This would be useful when I want to copy paste amazing_printed output to a documentation for a FE developers.

@HarlemSquirrel
Copy link
Member

@equivalent Sorry for the late response but we can set the ruby19_syntax: true option but this only works for symbols right now.

I think we definitely could add something like a json_hashes option for this use case.

@HarlemSquirrel HarlemSquirrel added the enhancement New feature or request label Oct 6, 2021
@HarlemSquirrel
Copy link
Member

Or maybe we move has formatting to a new hash_format option with three possibilities like rocket, symbol, and json

@paddor
Copy link
Contributor

paddor commented Oct 6, 2021

I like the idea of hash_format:

:rocket would work for all keys.

:symbol would only affect Symbol keys, while any other keys would be printed in rocket format. Ruby actually allows mixing of Hash literals: {a: 5, :b => 6, 'c' => 4, 2 => 1 }

And :json would convert every key into a String (#to_s) first and then print in JSON format. Should we care about possible duplicate keys? E.g. "a" and :a or "1" and 1 would turn into the same String keys.

@HarlemSquirrel
Copy link
Member

It looks like the built in JSON library returns those as duplicate string keys (at least on Ruby 3.0) and converts numbers to strings.

require 'json'
{ a: 'cheese', 'a' => 'burger', 1 => 'fries',  1.0 => 'version' }.to_json
# => "{\"a\":\"cheese\",\"a\":\"burger\",\"1\":\"fries\",\"1.0\":\"version\"}"

@paddor
Copy link
Contributor

paddor commented Oct 6, 2021

I just realized it will only matter when you’re trying to copy paste it into a script or console. And then it’s probably just the last duplicate that wins (if not a syntax error).

What I mean: You’ll still see all the keys if it’s simply used for (debug) logging.

@HarlemSquirrel HarlemSquirrel self-assigned this Oct 7, 2021
@HarlemSquirrel
Copy link
Member

I started working on this a bit and realized we'll want Array to also use JSON format. I'm not sure if hash_format is the best approach for this in that case.

HarlemSquirrel added a commit that referenced this issue Oct 7, 2021
@HarlemSquirrel HarlemSquirrel linked a pull request Oct 7, 2021 that will close this issue
HarlemSquirrel added a commit that referenced this issue Oct 7, 2021
Closes #70

Replace :ruby19_syntax option with new :hash_format option that has three possible settings.
@paddor
Copy link
Contributor

paddor commented Oct 7, 2021

What about output_syntax? Could be default, ruby (no array indexes to make it copy-pastable), json, ...

@HarlemSquirrel
Copy link
Member

The main problem here would be nested objects that can't be easily converted to JSON friendly literals. This reminds me of the strict args Sidekiq introduced with Sidekiq 6 and now requires with version 7.

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

Successfully merging a pull request may close this issue.

3 participants