Skip to content

Commit 37bee34

Browse files
wwqgtxxnekohasekai
authored andcommitted
Recover context json for go1.20
1 parent c86c253 commit 37bee34

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

common/json/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build go1.21 && !without_contextjson
1+
//go:build go1.20 && !without_contextjson
22

33
package json
44

common/json/internal/contextjson/encode.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
442442
b, err := m.MarshalJSON()
443443
if err == nil {
444444
e.Grow(len(b))
445-
out := e.AvailableBuffer()
445+
out := availableBuffer(&e.Buffer)
446446
out, err = appendCompact(out, b, opts.escapeHTML)
447447
e.Buffer.Write(out)
448448
}
@@ -461,7 +461,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
461461
b, err := m.MarshalJSON()
462462
if err == nil {
463463
e.Grow(len(b))
464-
out := e.AvailableBuffer()
464+
out := availableBuffer(&e.Buffer)
465465
out, err = appendCompact(out, b, opts.escapeHTML)
466466
e.Buffer.Write(out)
467467
}
@@ -484,7 +484,7 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
484484
if err != nil {
485485
e.error(&MarshalerError{v.Type(), err, "MarshalText"})
486486
}
487-
e.Write(appendString(e.AvailableBuffer(), b, opts.escapeHTML))
487+
e.Write(appendString(availableBuffer(&e.Buffer), b, opts.escapeHTML))
488488
}
489489

490490
func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
@@ -498,27 +498,27 @@ func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
498498
if err != nil {
499499
e.error(&MarshalerError{v.Type(), err, "MarshalText"})
500500
}
501-
e.Write(appendString(e.AvailableBuffer(), b, opts.escapeHTML))
501+
e.Write(appendString(availableBuffer(&e.Buffer), b, opts.escapeHTML))
502502
}
503503

504504
func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
505-
b := e.AvailableBuffer()
505+
b := availableBuffer(&e.Buffer)
506506
b = mayAppendQuote(b, opts.quoted)
507507
b = strconv.AppendBool(b, v.Bool())
508508
b = mayAppendQuote(b, opts.quoted)
509509
e.Write(b)
510510
}
511511

512512
func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
513-
b := e.AvailableBuffer()
513+
b := availableBuffer(&e.Buffer)
514514
b = mayAppendQuote(b, opts.quoted)
515515
b = strconv.AppendInt(b, v.Int(), 10)
516516
b = mayAppendQuote(b, opts.quoted)
517517
e.Write(b)
518518
}
519519

520520
func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
521-
b := e.AvailableBuffer()
521+
b := availableBuffer(&e.Buffer)
522522
b = mayAppendQuote(b, opts.quoted)
523523
b = strconv.AppendUint(b, v.Uint(), 10)
524524
b = mayAppendQuote(b, opts.quoted)
@@ -538,7 +538,7 @@ func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
538538
// See golang.org/issue/6384 and golang.org/issue/14135.
539539
// Like fmt %g, but the exponent cutoffs are different
540540
// and exponents themselves are not padded to two digits.
541-
b := e.AvailableBuffer()
541+
b := availableBuffer(&e.Buffer)
542542
b = mayAppendQuote(b, opts.quoted)
543543
abs := math.Abs(f)
544544
fmt := byte('f')
@@ -577,7 +577,7 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
577577
if !isValidNumber(numStr) {
578578
e.error(fmt.Errorf("json: invalid number literal %q", numStr))
579579
}
580-
b := e.AvailableBuffer()
580+
b := availableBuffer(&e.Buffer)
581581
b = mayAppendQuote(b, opts.quoted)
582582
b = append(b, numStr...)
583583
b = mayAppendQuote(b, opts.quoted)
@@ -586,9 +586,9 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
586586
}
587587
if opts.quoted {
588588
b := appendString(nil, v.String(), opts.escapeHTML)
589-
e.Write(appendString(e.AvailableBuffer(), b, false)) // no need to escape again since it is already escaped
589+
e.Write(appendString(availableBuffer(&e.Buffer), b, false)) // no need to escape again since it is already escaped
590590
} else {
591-
e.Write(appendString(e.AvailableBuffer(), v.String(), opts.escapeHTML))
591+
e.Write(appendString(availableBuffer(&e.Buffer), v.String(), opts.escapeHTML))
592592
}
593593
}
594594

@@ -754,7 +754,7 @@ func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
754754
if i > 0 {
755755
e.WriteByte(',')
756756
}
757-
e.Write(appendString(e.AvailableBuffer(), kv.ks, opts.escapeHTML))
757+
e.Write(appendString(availableBuffer(&e.Buffer), kv.ks, opts.escapeHTML))
758758
e.WriteByte(':')
759759
me.elemEnc(e, kv.v, opts)
760760
}
@@ -786,7 +786,7 @@ func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) {
786786
e.Grow(len(`"`) + encodedLen + len(`"`))
787787

788788
// TODO(https://go.dev/issue/53693): Use base64.Encoding.AppendEncode.
789-
b := e.AvailableBuffer()
789+
b := availableBuffer(&e.Buffer)
790790
b = append(b, '"')
791791
base64.StdEncoding.Encode(b[len(b):][:encodedLen], s)
792792
b = b[:len(b)+encodedLen]

common/json/internal/contextjson/indent.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ package json
66

77
import "bytes"
88

9+
// TODO(https://go.dev/issue/53685): Use bytes.Buffer.AvailableBuffer instead.
10+
func availableBuffer(b *bytes.Buffer) []byte {
11+
return b.Bytes()[b.Len():]
12+
}
13+
914
// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
1015
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
1116
// so that the JSON will be safe to embed inside HTML <script> tags.
1217
// For historical reasons, web browsers don't honor standard HTML
1318
// escaping within <script> tags, so an alternative JSON encoding must be used.
1419
func HTMLEscape(dst *bytes.Buffer, src []byte) {
1520
dst.Grow(len(src))
16-
dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src))
21+
dst.Write(appendHTMLEscape(availableBuffer(dst), src))
1722
}
1823

1924
func appendHTMLEscape(dst, src []byte) []byte {
@@ -40,7 +45,7 @@ func appendHTMLEscape(dst, src []byte) []byte {
4045
// insignificant space characters elided.
4146
func Compact(dst *bytes.Buffer, src []byte) error {
4247
dst.Grow(len(src))
43-
b := dst.AvailableBuffer()
48+
b := availableBuffer(dst)
4449
b, err := appendCompact(b, src, false)
4550
dst.Write(b)
4651
return err
@@ -109,7 +114,7 @@ const indentGrowthFactor = 2
109114
// if src ends in a trailing newline, so will dst.
110115
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
111116
dst.Grow(indentGrowthFactor * len(src))
112-
b := dst.AvailableBuffer()
117+
b := availableBuffer(dst)
113118
b, err := appendIndent(b, src, prefix, indent)
114119
dst.Write(b)
115120
return err

common/json/std.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !go1.21 || without_contextjson
1+
//go:build !go1.20 || without_contextjson
22

33
package json
44

0 commit comments

Comments
 (0)