Skip to content

Commit

Permalink
fixes #12 turn off HTML escaping for JSON marshalling and add test to…
Browse files Browse the repository at this point in the history
… verify this
  • Loading branch information
andy-miracl committed Jan 16, 2018
1 parent a6a13cd commit 1844ca0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 6 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ func tomlUnmarshal(data []byte, out interface{}) error {
return nil
}

func jsonMarshal(in interface{}) ([]byte, error) {
data, err := json.Marshal(in)
func jsonMarshal(data interface{}) ([]byte, error) {
buffer := bytes.Buffer{}
encoder := json.NewEncoder(&buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(data)
if err != nil {
return nil, wrapError(err, "The data could not be marshalled to json")
}
return data, nil
return buffer.Bytes(), nil
}

func yamlMarshal(in interface{}) ([]byte, error) {
Expand Down
10 changes: 6 additions & 4 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ func TestTomlMarshal_Error(t *testing.T) {
// --------

var (
testMarshalData = map[string]interface{}{"key": "value"}
testValue = `value!£$%^&*()_+-={}[]:@~;'#<>?,./|`
testMarshalData = map[string]interface{}{"key": testValue}
testMarshalDataInvalid = func() {}
testMarshalJSON = []byte(`{"key":"value"}`)
testMarshalYAML = []byte(`key: value
testMarshalJSON = []byte(`{"key":"` + testValue + `"}
`)
testMarshalTOML = []byte(`key = "value"
testMarshalYAML = []byte(`key: ` + testValue + `
`)
testMarshalTOML = []byte(`key = "` + testValue + `"
`)
testMarshalInvalid = []byte(`{invalid`)
)

0 comments on commit 1844ca0

Please sign in to comment.