Skip to content

Commit 9b0ac1d

Browse files
committed
encoding/json: fix and optimize marshal for quoted string
Since Go 1.2 every string can be marshaled to JSON without error even if it contains invalid UTF-8 byte sequences. Therefore there is no need to use Marshal again for the only reason of enclosing the string in double quotes. Not using Marshal here also removes the error check as there has not been a way for Marshal to fail anyway. name old time/op new time/op delta Issue34127-4 360ns ± 3% 200ns ± 3% -44.56% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Issue34127-4 56.0B ± 0% 40.0B ± 0% -28.57% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Issue34127-4 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Fixes #34154
1 parent 547021d commit 9b0ac1d

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/encoding/json/bench_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ func BenchmarkIssue10335(b *testing.B) {
297297
})
298298
}
299299

300+
func BenchmarkIssue34127(b *testing.B) {
301+
b.ReportAllocs()
302+
j := struct {
303+
Bar string `json:"bar,string"`
304+
}{
305+
Bar: `foobar`,
306+
}
307+
b.RunParallel(func(pb *testing.PB) {
308+
for pb.Next() {
309+
if _, err := Marshal(&j); err != nil {
310+
b.Fatal(err)
311+
}
312+
}
313+
})
314+
}
315+
300316
func BenchmarkUnmapped(b *testing.B) {
301317
b.ReportAllocs()
302318
j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)

src/encoding/json/encode.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,11 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
601601
return
602602
}
603603
if opts.quoted {
604-
sb, err := Marshal(v.String())
605-
if err != nil {
606-
e.error(err)
607-
}
608-
e.string(string(sb), opts.escapeHTML)
604+
b := make([]byte, 0, v.Len()+2)
605+
b = append(b, '"')
606+
b = append(b, []byte(v.String())...)
607+
b = append(b, '"')
608+
e.stringBytes(b, opts.escapeHTML)
609609
} else {
610610
e.string(v.String(), opts.escapeHTML)
611611
}

src/encoding/json/stream_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
118118
Ptr strPtrMarshaler
119119
}{`"<str>"`, `"<str>"`}
120120

121+
// https://golang.org/issue/34154
122+
stringOption := struct {
123+
Bar string `json:"bar,string"`
124+
}{`<html>foobar</html>`}
125+
121126
for _, tt := range []struct {
122127
name string
123128
v interface{}
@@ -137,6 +142,11 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
137142
`{"NonPtr":"\u003cstr\u003e","Ptr":"\u003cstr\u003e"}`,
138143
`{"NonPtr":"<str>","Ptr":"<str>"}`,
139144
},
145+
{
146+
"stringOption", stringOption,
147+
`{"bar":"\"\u003chtml\u003efoobar\u003c/html\u003e\""}`,
148+
`{"bar":"\"<html>foobar</html>\""}`,
149+
},
140150
} {
141151
var buf bytes.Buffer
142152
enc := NewEncoder(&buf)

0 commit comments

Comments
 (0)