Skip to content

Commit b4ee365

Browse files
committed
fix(types-json): Disable HTML escaping during JSON marshalling
1 parent d0c2e26 commit b4ee365

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

schema/json.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,17 @@ func (dst *JSON) Set(src any) error {
124124
return &ValidationError{Type: TypeJSON, Msg: "use pointer to JSON instead of value", Value: value}
125125

126126
default:
127-
buf, err := json.Marshal(value)
127+
buffer := &bytes.Buffer{}
128+
encoder := json.NewEncoder(buffer)
129+
encoder.SetEscapeHTML(false)
130+
err := encoder.Encode(value)
128131
if err != nil {
129132
return err
130133
}
131134

135+
buf := buffer.Bytes()
132136
// For map and slice jsons, it is easier for users to work with '[]' or '{}' instead of JSON's 'null'.
133-
if bytes.Equal(buf, []byte(`null`)) {
137+
if bytes.Equal(buf, []byte("null\n")) {
134138
if isEmptyStringMap(value) {
135139
*dst = JSON{Bytes: []byte("{}"), Status: Present}
136140
return nil

schema/json_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func TestJSONSet(t *testing.T) {
4343
{source: map[string]Foo{}, result: JSON{Bytes: []byte(`{}`), Status: Present}},
4444

4545
{source: nil, result: JSON{Status: Null}},
46+
47+
{source: map[string]any{"test1": "a&b", "test2": "😀"}, result: JSON{Bytes: []byte(`{"test1": "a&b", "test2": "😀"}`), Status: Present}},
4648
}
4749

4850
for i, tt := range successfulTests {

0 commit comments

Comments
 (0)