Reviewers: Ian Lance Taylor
Russ Cox uploaded a change:
https://go-review.googlesource.com/23295
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
---
M src/encoding/json/stream.go
M src/encoding/json/stream_test.go
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/encoding/json/stream.go b/src/encoding/json/stream.go
index a362704..8686784 100644
--- a/src/encoding/json/stream.go
+++ b/src/encoding/json/stream.go
@@ -219,9 +219,15 @@
return err
}
-// Indent sets the encoder to format each encoded value with Indent.
-func (enc *Encoder) Indent(prefix, indent string) {
- enc.indentBuf = new(bytes.Buffer)
+// SetIndent instructs the encoder to format each subsequent encoded
+// value as if indented by the package-level function Indent(prefix,
indent).
+// It is not possible to subsequently disable indentation:
+// even Indent("", "") will still insert additional newline characters
before each
+// element in a JSON object or array.
+func (enc *Encoder) SetIndent(prefix, indent string) {
+ if enc.indentBuf == nil {
+ enc.indentBuf = new(bytes.Buffer)
+ }
enc.indentPrefix = prefix
enc.indentValue = indent
}
diff --git a/src/encoding/json/stream_test.go
b/src/encoding/json/stream_test.go
index 0d578ce..ed6a391 100644
--- a/src/encoding/json/stream_test.go
+++ b/src/encoding/json/stream_test.go
@@ -77,7 +77,7 @@
func TestEncoderIndent(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)
- enc.Indent(">", ".")
+ enc.SetIndent(">", ".")
for _, v := range streamTest {
enc.Encode(v)
}
--
https://go-review.googlesource.com/23295
Gerrit-Reviewer: Ian Lance Taylor <
ia...@golang.org>
Gerrit-Reviewer: Russ Cox <
r...@golang.org>