Skip to content

Commit

Permalink
Merge pull request #144 from hairyhenderson/nested-json-marshaling-138
Browse files Browse the repository at this point in the history
Using  github.com/ugorji/go/codec for JSON encoding instead of encoding/json
  • Loading branch information
hairyhenderson authored May 20, 2017
2 parents 7a66f5b + d6ec657 commit 816ba49
Show file tree
Hide file tree
Showing 52 changed files with 57,022 additions and 17 deletions.
8 changes: 6 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import:
- package: github.com/blang/vfs
version: c14afca
- package: gopkg.in/yaml.v2
repo: https://github.com/go-yaml/yaml.git
version: v2
repo: https://github.com/go-yaml/yaml.git
- package: github.com/spf13/cobra
- package: github.com/spf13/pflag
- package: github.com/ugorji/go
subpackages:
- codec
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
Expand Down
28 changes: 21 additions & 7 deletions test/integration/typeconv_funcs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ function teardown () {
[[ "${output}" == "true" ]]
}

@test "toJSONPretty" {
gomplate -i '{{ `{"hello": "world"}` | json | toJSONPretty " " }}
{{ toJSONPretty "" (`{"hello": "world"}` | json) }}'
@test "'toJSON' can handle nested maps" {
gomplate -i '{{ "foo:\n bar:\n baz: qux" | yaml | toJSON }}'
[ "$status" -eq 0 ]
[[ "${output}" == "{
\"hello\": \"world\"
[[ "${output}" == '{"foo":{"bar":{"baz":"qux"}}}' ]]
}

@test "'toJSONPretty' can handle nested maps" {
gomplate -i '{{ `{"foo":{"bar":{"baz":"qux"}}}` | json | toJSONPretty " " }}
{{ toJSONPretty "" (`{"foo":{"bar":{"baz":"qux"}}}` | json) }}'
[ "$status" -eq 0 ]
[[ "${output}" == '{
"foo": {
"bar": {
"baz": "qux"
}
}
}
{
\"hello\": \"world\"
}" ]]
"foo": {
"bar": {
"baz": "qux"
}
}
}' ]]
}

@test "indent" {
Expand Down
24 changes: 20 additions & 4 deletions typeconv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand All @@ -9,6 +10,8 @@ import (
"strings"

yaml "gopkg.in/yaml.v2"

"github.com/ugorji/go/codec"
)

// TypeConv - type conversion function
Expand Down Expand Up @@ -74,19 +77,32 @@ func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
return string(b)
}

func toJSONBytes(in interface{}) []byte {
h := &codec.JsonHandle{}
h.Canonical = true
buf := new(bytes.Buffer)
err := codec.NewEncoder(buf, h).Encode(in)
if err != nil {
log.Fatalf("Unable to marshal %s: %v", in, err)
}
return buf.Bytes()
}

// ToJSON - Stringify a struct as JSON
func (t *TypeConv) ToJSON(in interface{}) string {
return marshalObj(in, json.Marshal)
return string(toJSONBytes(in))
}

// ToJSONPretty - Stringify a struct as JSON (indented)
func (t *TypeConv) toJSONPretty(indent string, in interface{}) string {
b, err := json.MarshalIndent(in, "", indent)
out := new(bytes.Buffer)
b := toJSONBytes(in)
err := json.Indent(out, b, "", indent)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", in, err)
log.Fatalf("Unable to indent JSON %s: %v", b, err)
}

return string(b)
return string(out.Bytes())
}

// ToYAML - Stringify a struct as YAML
Expand Down
6 changes: 3 additions & 3 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func TestToJSON(t *testing.T) {
"foo": "bar",
"one": 1,
"true": true,
"down": map[string]interface{}{
"the": map[string]interface{}{
"rabbit": map[string]interface{}{
"down": map[interface{}]interface{}{
"the": map[interface{}]interface{}{
"rabbit": map[interface{}]interface{}{
"hole": true,
},
},
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/ugorji/go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/ugorji/go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions vendor/github.com/ugorji/go/codec/0doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 816ba49

Please sign in to comment.