Skip to content
Open
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
16 changes: 14 additions & 2 deletions sjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package sjson

import (
"bytes"
jsongo "encoding/json"
"sort"
"strconv"
Expand Down Expand Up @@ -123,7 +124,7 @@ func mustMarshalString(s string) bool {
// appendStringify makes a json string and appends to buf.
func appendStringify(buf []byte, s string) []byte {
if mustMarshalString(s) {
b, _ := jsongo.Marshal(s)
b, _ := marshal(s)
return append(buf, b...)
}
buf = append(buf, '"')
Expand Down Expand Up @@ -671,7 +672,7 @@ func SetBytesOptions(json []byte, path string, value interface{},
var err error
switch v := value.(type) {
default:
b, merr := jsongo.Marshal(value)
b, merr := marshal(value)
if merr != nil {
return nil, merr
}
Expand Down Expand Up @@ -745,3 +746,14 @@ func SetRawBytesOptions(json []byte, path string, value []byte,
}
return res, err
}

func marshal(v interface{}) ([]byte, error) {
var buffer bytes.Buffer
encoder := jsongo.NewEncoder(&buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
return bytes.TrimSpace(buffer.Bytes()), nil
}
8 changes: 8 additions & 0 deletions sjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,11 @@ func TestIssue61(t *testing.T) {
t.Fail()
}
}

func TestIssue71(t *testing.T) {
expected := `{"data":"{\"text\":\"<p>test</p>\"}"}`
got, _ := Set(``, "data", `{"text":"<p>test</p>"}`)
if expected != got {
t.Fatalf("expected '%v', got '%v'", expected, got)
}
}