Skip to content

encoding/json: prevent compact twice to improve precomputed performance. #35320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 14 additions & 4 deletions src/encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ type encOpts struct {
quoted bool
// escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
escapeHTML bool
// trustMarshaler causes Marshaler not to be compacted, escaped and validated.
trustMarshaler bool
}

type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
Expand Down Expand Up @@ -462,8 +464,12 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
}
b, err := m.MarshalJSON()
if err == nil {
// copy JSON into buffer, checking validity.
err = compact(&e.Buffer, b, opts.escapeHTML)
// copy JSON in to buffer.
if opts.trustMarshaler {
_, err = e.Buffer.Write(b)
} else {
err = compact(&e.Buffer, b, opts.escapeHTML)
}
}
if err != nil {
e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
Expand All @@ -479,8 +485,12 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
m := va.Interface().(Marshaler)
b, err := m.MarshalJSON()
if err == nil {
// copy JSON into buffer, checking validity.
err = compact(&e.Buffer, b, opts.escapeHTML)
// copy JSON in to buffer.
if opts.trustMarshaler {
_, err = e.Buffer.Write(b)
} else {
err = compact(&e.Buffer, b, opts.escapeHTML)
}
}
if err != nil {
e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
Expand Down
18 changes: 14 additions & 4 deletions src/encoding/json/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ func nonSpace(b []byte) bool {

// An Encoder writes JSON values to an output stream.
type Encoder struct {
w io.Writer
err error
escapeHTML bool
w io.Writer
err error
escapeHTML bool
trustMarshaler bool

indentBuf *bytes.Buffer
indentPrefix string
Expand All @@ -203,7 +204,7 @@ func (enc *Encoder) Encode(v interface{}) error {
return enc.err
}
e := newEncodeState()
err := e.marshal(v, encOpts{escapeHTML: enc.escapeHTML})
err := e.marshal(v, encOpts{escapeHTML: enc.escapeHTML, trustMarshaler: enc.trustMarshaler})
if err != nil {
return err
}
Expand Down Expand Up @@ -254,6 +255,15 @@ func (enc *Encoder) SetEscapeHTML(on bool) {
enc.escapeHTML = on
}

// SetTrustMarshaler specifies whether fields implement Marshaler
// should be computed such as escape, compact.
// The default behavior is to run compact on output of Marshaler.
// SetTrustMarshaler(true) just copies output of Marshaler
// and makes better performance.
func (enc *Encoder) SetTrustMarshaler(on bool) {
enc.trustMarshaler = on
}

// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
Expand Down