Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions schema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down