mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 02:52:14 -05:00
parent
4a366fcfee
commit
5a52cd5f92
4 changed files with 5 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
title: jsonify
|
title: jsonify
|
||||||
linktitle: jsonify
|
linktitle: jsonify
|
||||||
description: Encodes a given object to JSON.
|
description: Encodes a given object to JSON, returning pretty printed output.
|
||||||
godocref:
|
godocref:
|
||||||
date: 2017-02-01
|
date: 2017-02-01
|
||||||
publishdate: 2017-02-01
|
publishdate: 2017-02-01
|
||||||
|
|
|
@ -50,9 +50,9 @@ func (ns *Namespace) Base64Encode(content interface{}) (string, error) {
|
||||||
return base64.StdEncoding.EncodeToString([]byte(conv)), nil
|
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) {
|
func (ns *Namespace) Jsonify(v interface{}) (template.HTML, error) {
|
||||||
b, err := json.Marshal(v)
|
b, err := json.MarshalIndent(v, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ func TestJsonify(t *testing.T) {
|
||||||
v interface{}
|
v interface{}
|
||||||
expect interface{}
|
expect interface{}
|
||||||
}{
|
}{
|
||||||
{[]string{"a", "b"}, template.HTML(`["a","b"]`)},
|
{[]string{"a", "b"}, template.HTML("[\n \"a\",\n \"b\"\n]")},
|
||||||
{tstNoStringer{}, template.HTML("{}")},
|
{tstNoStringer{}, template.HTML("{}")},
|
||||||
{nil, template.HTML("null")},
|
{nil, template.HTML("null")},
|
||||||
// errors
|
// errors
|
||||||
|
|
|
@ -47,7 +47,7 @@ func init() {
|
||||||
ns.AddMethodMapping(ctx.Jsonify,
|
ns.AddMethodMapping(ctx.Jsonify,
|
||||||
[]string{"jsonify"},
|
[]string{"jsonify"},
|
||||||
[][2]string{
|
[][2]string{
|
||||||
{`{{ (slice "A" "B" "C") | jsonify }}`, `["A","B","C"]`},
|
{`{{ (slice "A" "B" "C") | jsonify }}`, "[\n \"A\",\n \"B\",\n \"C\"\n]"},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue