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

Allow "jsonify" to pretty print JSON #5040

Closed
hach-que opened this issue Aug 6, 2018 · 10 comments · Fixed by #7082 or #5339
Closed

Allow "jsonify" to pretty print JSON #5040

hach-que opened this issue Aug 6, 2018 · 10 comments · Fixed by #7082 or #5339

Comments

@hach-que
Copy link

hach-que commented Aug 6, 2018

"jsonify" currently uses json.Marshal which emits compact JSON.

We'd like to have the JSON output pretty printed (we're using Hugo for API documentation), so we need some function in Hugo that exposes json.MarshalIndent.

@bep
Copy link
Member

bep commented Aug 6, 2018

I'm tempted to change the existing one to use json.MarshalIndent by default with a sensible indentation (2 spaces?) as default.

I assume we could also add a second optional argument for indentation:

{{ (slice "A" "B" "C") | jsonify "   " }}

@bep bep added the Enhancement label Aug 6, 2018
@bep bep modified the milestones: v0.47, v0.48 Aug 6, 2018
@bep bep modified the milestones: v0.48, v0.49 Aug 22, 2018
@bep bep modified the milestones: v0.49, v0.50 Sep 13, 2018
@bep bep modified the milestones: v0.50, v0.51 Oct 6, 2018
@seanprashad
Copy link
Contributor

Hey guys! I'd love to land this new feature if that's ok 😬

@bep
Copy link
Member

bep commented Nov 2, 2018

@seanprashad I had to reopen this. I suggest we add an optional second argument so both of the below will work:

{{ (slice "A" "B" "C") | jsonify }}
{{ (slice "A" "B" "C") | jsonify "   " }}

@bep bep modified the milestones: v0.51, v0.53 Nov 8, 2018
@bep bep modified the milestones: v0.53, v0.54 Dec 6, 2018
@bep bep modified the milestones: v0.54, v0.55 Dec 26, 2018
@bep bep modified the milestones: v0.55, v0.56 Feb 1, 2019
@stale
Copy link

stale bot commented Jun 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Jun 1, 2019
@bep bep added this to the v0.61 milestone Oct 21, 2019
@bep bep modified the milestones: v0.61, v0.62, v0.63 Nov 25, 2019
@bep bep modified the milestones: v0.63, v0.64 Dec 11, 2019
@bep bep modified the milestones: v0.64, v0.65 Jan 22, 2020
@bep bep modified the milestones: v0.65, v0.66 Jan 30, 2020
@bep bep modified the milestones: v0.66, v0.67 Mar 2, 2020
@bep bep modified the milestones: v0.67, v0.68 Mar 9, 2020
@bep bep modified the milestones: v0.68, v0.69 Mar 20, 2020
@zbayoff
Copy link
Contributor

zbayoff commented Mar 21, 2020

Also looking for this feature, as we're trying to use the jsonify function with the highlight function, but can't get the json to come out legible. Also using jsonify for API Documentation purposes.

@moorereason
Copy link
Contributor

@bep has suggested a different approach to what's been discussed here. His comment:

...thinking about it now I think I would prefer to use a dict option, e.g. $foo | (dict "indent" " ")

Esp. if we should get more options in the future.

@bep means this:

{{ (slice "A" "B" "C") | jsonify (dict "indent" "  ") }}

I agree about future options, but I think the main two use cases were covered in the previous discussions. However, we can support all three cases:

{{ (slice "A" "B" "C") | jsonify }}
{{ (slice "A" "B" "C") | jsonify "  " }}
{{ (slice "A" "B" "C") | jsonify (dict "indent" "  " "prefix" "> ") }}

We can easily tell if the argument is a map and handle that case for advanced usages. However, I'd suggest we hold off on the complexity until there's a need for it.

Thoughts?

@bep
Copy link
Member

bep commented Mar 21, 2020

@moorereason you are mostly right ... but this:

{{ (slice "A" "B" "C") | jsonify }}
{{ (slice "A" "B" "C") | jsonify "  " }}
{{ (slice "A" "B" "C") | jsonify (dict "indent" "  " "prefix" "> ") }}

while it works, it's more complex from a maintenance and documentation perspective than this:

{{ (slice "A" "B" "C") | jsonify }}
{{ (slice "A" "B" "C") | jsonify (dict "indent" "  " "prefix" "> ") }}

In isolation, it may not be much, but it adds up when you look at it on scale.

Our dict support is currently a little limited (I hope we can get newline support one day ...), but it does allow you read it in from params etc., so in general it works OK.@

@davidejones
Copy link
Contributor

+1 for this, it would help in a few places we are displaying json to a user.

@zbayoff
Copy link
Contributor

zbayoff commented Apr 7, 2020

any updates on the open PR for this feature: #7082 ?

@github-actions
Copy link

github-actions bot commented Feb 4, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants