Skip to content

Commit

Permalink
tpl: Update Jsonify to return pretty-print output
Browse files Browse the repository at this point in the history
Fixes #5040
  • Loading branch information
Sean Prashad authored and bep committed Oct 21, 2018
1 parent 4a366fc commit 5a52cd5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/content/en/functions/jsonify.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: jsonify
linktitle: jsonify
description: Encodes a given object to JSON.
description: Encodes a given object to JSON, returning pretty printed output.
godocref:
date: 2017-02-01
publishdate: 2017-02-01
Expand Down
4 changes: 2 additions & 2 deletions tpl/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (ns *Namespace) Base64Encode(content interface{}) (string, error) {
return base64.StdEncoding.EncodeToString([]byte(conv)), nil
}

// Jsonify encodes a given object to JSON.
// Jsonify encodes a given object to JSON, returning pretty printed output.
func (ns *Namespace) Jsonify(v interface{}) (template.HTML, error) {
b, err := json.Marshal(v)
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion tpl/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestJsonify(t *testing.T) {
v interface{}
expect interface{}
}{
{[]string{"a", "b"}, template.HTML(`["a","b"]`)},
{[]string{"a", "b"}, template.HTML("[\n \"a\",\n \"b\"\n]")},
{tstNoStringer{}, template.HTML("{}")},
{nil, template.HTML("null")},
// errors
Expand Down
2 changes: 1 addition & 1 deletion tpl/encoding/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
ns.AddMethodMapping(ctx.Jsonify,
[]string{"jsonify"},
[][2]string{
{`{{ (slice "A" "B" "C") | jsonify }}`, `["A","B","C"]`},
{`{{ (slice "A" "B" "C") | jsonify }}`, "[\n \"A\",\n \"B\",\n \"C\"\n]"},
},
)

Expand Down

0 comments on commit 5a52cd5

Please sign in to comment.