Skip to content

Commit 34b17d4

Browse files
committed
encoding/json: rename Indent method to SetIndent
CL 21057 added this method during the Go 1.7 cycle (so it is not yet released and still possible to revise). This makes it clearer that the method is not doing something (like func Indent does), but just changing a setting about doing something later. Also document that this is in some sense irreversible. I think that's probably a mistake but the original CL discussion claimed it as a feature, so I'll leave it alone. For #6492. Change-Id: If4415c869a9196501056c143811a308822d5a420 Reviewed-on: https://go-review.googlesource.com/23295 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
1 parent 4aea7a1 commit 34b17d4

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/encoding/json/stream.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ func (enc *Encoder) Encode(v interface{}) error {
204204
e.WriteByte('\n')
205205

206206
b := e.Bytes()
207-
if enc.indentBuf != nil {
207+
if enc.indentPrefix != "" || enc.indentValue != "" {
208+
if enc.indentBuf == nil {
209+
enc.indentBuf = new(bytes.Buffer)
210+
}
208211
enc.indentBuf.Reset()
209212
err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue)
210213
if err != nil {
@@ -219,9 +222,10 @@ func (enc *Encoder) Encode(v interface{}) error {
219222
return err
220223
}
221224

222-
// Indent sets the encoder to format each encoded value with Indent.
223-
func (enc *Encoder) Indent(prefix, indent string) {
224-
enc.indentBuf = new(bytes.Buffer)
225+
// SetIndent instructs the encoder to format each subsequent encoded
226+
// value as if indented by the package-level function Indent(dst, src, prefix, indent).
227+
// Calling SetIndent("", "") disables indentation.
228+
func (enc *Encoder) SetIndent(prefix, indent string) {
225229
enc.indentPrefix = prefix
226230
enc.indentValue = indent
227231
}
@@ -230,7 +234,7 @@ func (enc *Encoder) Indent(prefix, indent string) {
230234
// should be escaped inside JSON quoted strings.
231235
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
232236
// to avoid certain safety problems that can arise when embedding JSON in HTML.
233-
//
237+
//
234238
// In non-HTML settings where the escaping interferes with the readability
235239
// of the output, SetEscapeHTML(false) disables this behavior.
236240
func (enc *Encoder) SetEscapeHTML(on bool) {

src/encoding/json/stream_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func TestEncoder(t *testing.T) {
4444
for i := 0; i <= len(streamTest); i++ {
4545
var buf bytes.Buffer
4646
enc := NewEncoder(&buf)
47+
// Check that enc.SetIndent("", "") turns off indentation.
48+
enc.SetIndent(">", ".")
49+
enc.SetIndent("", "")
4750
for j, v := range streamTest[0:i] {
4851
if err := enc.Encode(v); err != nil {
4952
t.Fatalf("encode #%d: %v", j, err)
@@ -77,7 +80,7 @@ false
7780
func TestEncoderIndent(t *testing.T) {
7881
var buf bytes.Buffer
7982
enc := NewEncoder(&buf)
80-
enc.Indent(">", ".")
83+
enc.SetIndent(">", ".")
8184
for _, v := range streamTest {
8285
enc.Encode(v)
8386
}

0 commit comments

Comments
 (0)