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 #5339

Merged
merged 1 commit into from
Oct 21, 2018
Merged

Allow "jsonify" to pretty print JSON #5339

merged 1 commit into from
Oct 21, 2018

Conversation

seanprashad
Copy link
Contributor

@seanprashad seanprashad commented Oct 21, 2018

Fixes #5040: Allow "jsonify" to pretty print JSON

This PR replaces json.Marshal with json.MarshalIndent, to return pretty-printed output instead of compact. A sensible spacing of two spaces has chosen for the indentation.

@CLAassistant
Copy link

CLAassistant commented Oct 21, 2018

CLA assistant check
All committers have signed the CLA.

indentSpacing = " "
}

b, err := json.MarshalIndent(v, "", indentSpacing)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting some very weird behaviour here - when I switched to json.MarshalIndent and updated the respective table test, there was a panic occurring. I was looking into the docs for template.HTML but everything looked ok at first glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently running on go version go1.11.1 darwin/amd64 if that helps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: template.HTML("[\n \"a\",\n \"b\"\n]") was the answer - I had to escape quotes

@seanprashad
Copy link
Contributor Author

seanprashad commented Oct 21, 2018

Small snippet for how json.MarshalIndent works:

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

func main() {

	data := []string{"A", "B", "C"}

	json, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(json))
}

prints:

[
  "A",
  "B",
  "C"
]

@bep bep merged commit 5a52cd5 into gohugoio:master Oct 21, 2018
@bep
Copy link
Member

bep commented Oct 21, 2018

Thanks for this, and a special thanks for keeping the commit message in line ... so I could just click the button

@bep bep mentioned this pull request Oct 21, 2018
@seanprashad seanprashad deleted the issue-5040 branch October 21, 2018 22:28
@github-actions
Copy link

github-actions bot commented Feb 1, 2022

This pull request 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 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow "jsonify" to pretty print JSON
3 participants