diff --git a/schema/json.go b/schema/json.go index b4c6c32610..8ca70ec46a 100644 --- a/schema/json.go +++ b/schema/json.go @@ -124,11 +124,16 @@ func (dst *JSON) Set(src any) error { return &ValidationError{Type: TypeJSON, Msg: "use pointer to JSON instead of value", Value: value} default: - buf, err := json.Marshal(value) + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + err := encoder.Encode(value) if err != nil { return err } + // JSON encoder adds a newline to the end of the output that we don't want. + buf := bytes.TrimSuffix(buffer.Bytes(), []byte("\n")) // For map and slice jsons, it is easier for users to work with '[]' or '{}' instead of JSON's 'null'. if bytes.Equal(buf, []byte(`null`)) { if isEmptyStringMap(value) { diff --git a/schema/json_test.go b/schema/json_test.go index 3cbd53fc7e..fffdb66773 100644 --- a/schema/json_test.go +++ b/schema/json_test.go @@ -43,6 +43,8 @@ func TestJSONSet(t *testing.T) { {source: map[string]Foo{}, result: JSON{Bytes: []byte(`{}`), Status: Present}}, {source: nil, result: JSON{Status: Null}}, + + {source: map[string]any{"test1": "a&b", "test2": "😀"}, result: JSON{Bytes: []byte(`{"test1": "a&b", "test2": "😀"}`), Status: Present}}, } for i, tt := range successfulTests {