Commit fb5b069d authored by Mitar's avatar Mitar
Browse files

Add MarshalWithoutEscapeHTML.

parent a3c7332c
Pipeline #462728308 passed with stages
in 2 minutes and 15 seconds
......@@ -18,3 +18,16 @@ func UnmarshalWithoutUnknownFields(data []byte, v interface{}) errors.E {
}
return nil
}
// MarshalWithoutEscapeHTML is a standard JSON marshal, just that
// it does not escape HTML characters.
func MarshalWithoutEscapeHTML(v interface{}) ([]byte, errors.E) {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, errors.WithStack(err)
}
return buf.Bytes(), nil
}
......@@ -24,3 +24,13 @@ func TestUnmarshalWithoutUnknownFields(t *testing.T) {
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field2": "abc"}`), &v)
assert.Error(t, err)
}
func TestMarshalWithoutEscapeHTML(t *testing.T) {
type Test struct {
Field string `json:"field"`
}
data, err := x.MarshalWithoutEscapeHTML(&Test{Field: "<body></body>"})
assert.NoError(t, err)
assert.Equal(t, `{"field":"<body></body>"}`+"\n", string(data))
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment