diff options
author | Stephen McQuay <stephen@mcquay.me> | 2014-05-08 16:52:36 +1000 |
---|---|---|
committer | Stephen McQuay <stephen@mcquay.me> | 2014-05-08 16:52:36 +1000 |
commit | 3f5288186dfc0b733e6533fa32c19aad2cd80b9f (patch) | |
tree | 9e8a554e2a1808e788f6a0fafa5bfba74536d5dd /src | |
parent | 3908d605fec90519e9d9afc5bb481d7687b55e30 (diff) | |
download | go-3f5288186dfc0b733e6533fa32c19aad2cd80b9f.tar.gz |
encoding/json: add example for Indent, clarify the docs.
There was confusion in the behavior of json.Indent; This change
attempts to clarify the behavior by providing a bit more verbiage
to the documentation as well as provide an example function.
Fixes issue 7821.
LGTM=robert.hencke, adg
R=golang-codereviews, minux.ma, bradfitz, aram, robert.hencke, r, adg
CC=golang-codereviews
https://codereview.appspot.com/97840044
Committer: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/pkg/encoding/json/example_test.go | 32 | ||||
-rw-r--r-- | src/pkg/encoding/json/indent.go | 5 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/pkg/encoding/json/example_test.go b/src/pkg/encoding/json/example_test.go index ea0bc149c..ca4e5ae68 100644 --- a/src/pkg/encoding/json/example_test.go +++ b/src/pkg/encoding/json/example_test.go @@ -5,6 +5,7 @@ package json_test import ( + "bytes" "encoding/json" "fmt" "io" @@ -127,3 +128,34 @@ func ExampleRawMessage() { // YCbCr &{255 0 -10} // RGB &{98 218 255} } + +func ExampleIndent() { + type Road struct { + Name string + Number int + } + roads := []Road{ + {"Diamond Fork", 29}, + {"Sheep Creek", 51}, + } + + b, err := json.Marshal(roads) + if err != nil { + log.Fatal(err) + } + + var out bytes.Buffer + json.Indent(&out, b, "=", "\t") + out.WriteTo(os.Stdout) + // Output: + // [ + // = { + // = "Name": "Diamond Fork", + // = "Number": 29 + // = }, + // = { + // = "Name": "Sheep Creek", + // = "Number": 51 + // = } + // =] +} diff --git a/src/pkg/encoding/json/indent.go b/src/pkg/encoding/json/indent.go index 11ef709cc..e1bacafd6 100644 --- a/src/pkg/encoding/json/indent.go +++ b/src/pkg/encoding/json/indent.go @@ -69,8 +69,9 @@ func newline(dst *bytes.Buffer, prefix, indent string, depth int) { // Each element in a JSON object or array begins on a new, // indented line beginning with prefix followed by one or more // copies of indent according to the indentation nesting. -// The data appended to dst has no trailing newline, to make it easier -// to embed inside other formatted JSON data. +// The data appended to dst does not begin with the prefix nor +// any indentation, and has no trailing newline, to make it +// easier to embed inside other formatted JSON data. func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { origLen := dst.Len() var scan scanner |