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>
This commit is contained in:
Russ Cox 2016-05-23 12:28:56 -04:00
parent 4aea7a12b6
commit 34b17d4dc5
2 changed files with 13 additions and 6 deletions

View file

@ -204,7 +204,10 @@ func (enc *Encoder) Encode(v interface{}) error {
e.WriteByte('\n') e.WriteByte('\n')
b := e.Bytes() b := e.Bytes()
if enc.indentBuf != nil { if enc.indentPrefix != "" || enc.indentValue != "" {
if enc.indentBuf == nil {
enc.indentBuf = new(bytes.Buffer)
}
enc.indentBuf.Reset() enc.indentBuf.Reset()
err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue) err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue)
if err != nil { if err != nil {
@ -219,9 +222,10 @@ func (enc *Encoder) Encode(v interface{}) error {
return err return err
} }
// Indent sets the encoder to format each encoded value with Indent. // SetIndent instructs the encoder to format each subsequent encoded
func (enc *Encoder) Indent(prefix, indent string) { // value as if indented by the package-level function Indent(dst, src, prefix, indent).
enc.indentBuf = new(bytes.Buffer) // Calling SetIndent("", "") disables indentation.
func (enc *Encoder) SetIndent(prefix, indent string) {
enc.indentPrefix = prefix enc.indentPrefix = prefix
enc.indentValue = indent enc.indentValue = indent
} }
@ -230,7 +234,7 @@ func (enc *Encoder) Indent(prefix, indent string) {
// should be escaped inside JSON quoted strings. // should be escaped inside JSON quoted strings.
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e // The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
// to avoid certain safety problems that can arise when embedding JSON in HTML. // to avoid certain safety problems that can arise when embedding JSON in HTML.
// //
// In non-HTML settings where the escaping interferes with the readability // In non-HTML settings where the escaping interferes with the readability
// of the output, SetEscapeHTML(false) disables this behavior. // of the output, SetEscapeHTML(false) disables this behavior.
func (enc *Encoder) SetEscapeHTML(on bool) { func (enc *Encoder) SetEscapeHTML(on bool) {

View file

@ -44,6 +44,9 @@ func TestEncoder(t *testing.T) {
for i := 0; i <= len(streamTest); i++ { for i := 0; i <= len(streamTest); i++ {
var buf bytes.Buffer var buf bytes.Buffer
enc := NewEncoder(&buf) enc := NewEncoder(&buf)
// Check that enc.SetIndent("", "") turns off indentation.
enc.SetIndent(">", ".")
enc.SetIndent("", "")
for j, v := range streamTest[0:i] { for j, v := range streamTest[0:i] {
if err := enc.Encode(v); err != nil { if err := enc.Encode(v); err != nil {
t.Fatalf("encode #%d: %v", j, err) t.Fatalf("encode #%d: %v", j, err)
@ -77,7 +80,7 @@ false
func TestEncoderIndent(t *testing.T) { func TestEncoderIndent(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
enc := NewEncoder(&buf) enc := NewEncoder(&buf)
enc.Indent(">", ".") enc.SetIndent(">", ".")
for _, v := range streamTest { for _, v := range streamTest {
enc.Encode(v) enc.Encode(v)
} }