Hugo: Allow "jsonify" to pretty print JSON

Created on 6 Aug 2018  路  9Comments  路  Source: gohugoio/hugo

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

Enhancement

Most helpful comment

@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 "   " }}

All 9 comments

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 "   " }}

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

@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 "   " }}

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.

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.

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

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

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

any updates on the open PR for this feature: https://github.com/gohugoio/hugo/pull/7082 ?

Was this page helpful?
0 / 5 - 0 ratings