Skip to content

Commit 28d458b

Browse files
committed
Disable html escaping
This commit removes the automatic escaping of html characters, effectively rendering JSON strings in the same way as is the builtin Go encoder with SetEscapeHTML(false). This should not affect the quality of the resulting JSON and hopefully will not cause any downstream issues.
1 parent c2bc5a4 commit 28d458b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

gjson.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,9 +1940,6 @@ func AppendJSONString(dst []byte, s string) []byte {
19401940
dst = append(dst, 'u')
19411941
dst = appendHex16(dst, uint16(s[i]))
19421942
}
1943-
} else if s[i] == '>' || s[i] == '<' || s[i] == '&' {
1944-
dst = append(dst, '\\', 'u')
1945-
dst = appendHex16(dst, uint16(s[i]))
19461943
} else if s[i] == '\\' {
19471944
dst = append(dst, '\\', '\\')
19481945
} else if s[i] == '"' {

gjson_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,9 +2549,17 @@ func TestGroup(t *testing.T) {
25492549
assert(t, res == `["123"]`)
25502550
}
25512551

2552+
func goJSONMarshal(i interface{}) ([]byte, error) {
2553+
buffer := &bytes.Buffer{}
2554+
encoder := json.NewEncoder(buffer)
2555+
encoder.SetEscapeHTML(false)
2556+
err := encoder.Encode(i)
2557+
return bytes.TrimRight(buffer.Bytes(), "\n"), err
2558+
}
2559+
25522560
func testJSONString(t *testing.T, str string) {
25532561
gjsonString := string(AppendJSONString(nil, str))
2554-
data, err := json.Marshal(str)
2562+
data, err := goJSONMarshal(str)
25552563
if err != nil {
25562564
panic(123)
25572565
}
@@ -2579,7 +2587,7 @@ func TestJSONString(t *testing.T) {
25792587
testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr")
25802588
testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04")
25812589
data, _ := json.Marshal("\b\f")
2582-
if (string(data) == "\"\\b\\f\"") {
2590+
if string(data) == "\"\\b\\f\"" {
25832591
// Go version 1.22+ encodes "\b" and "\f" correctly.
25842592
testJSONString(t, "\b\f")
25852593
rng := rand.New(rand.NewSource(time.Now().UnixNano()))

0 commit comments

Comments
 (0)