Skip to content

Commit 2f6f1d8

Browse files
authored
1 parent dfb039d commit 2f6f1d8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

schema/json.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ 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+
// JSON encoder adds a newline to the end of the output that we don't want.
136+
buf := bytes.TrimSuffix(buffer.Bytes(), []byte("\n"))
132137
// For map and slice jsons, it is easier for users to work with '[]' or '{}' instead of JSON's 'null'.
133138
if bytes.Equal(buf, []byte(`null`)) {
134139
if isEmptyStringMap(value) {

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)