]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: update documentation for MarshalIndent
authortbunyk <tbunyk@gmail.com>
Tue, 5 Sep 2017 16:01:16 +0000 (19:01 +0300)
committerIan Lance Taylor <iant@golang.org>
Tue, 12 Sep 2017 18:12:24 +0000 (18:12 +0000)
Make arguments semantics clear without the need to look for
json.Indent documentation.

Change-Id: If9adfe9f477a30d426ae83790b0f2578c0a809b7
Reviewed-on: https://go-review.googlesource.com/61670
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/encoding/json/encode.go
src/encoding/json/example_test.go

index 141b23c6c63f5b9b7ff320adc1118caafd7ce966..9a2f84133544f7e5522d57a5fc87e3ed761a50c8 100644 (file)
@@ -166,6 +166,8 @@ func Marshal(v interface{}) ([]byte, error) {
 }
 
 // MarshalIndent is like Marshal but applies Indent to format the output.
+// Each JSON element in the output will begin on a new line beginning with prefix
+// followed by one or more copies of indent according to the indentation nesting.
 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
        b, err := Marshal(v)
        if err != nil {
index 2bbc292c6da1c359c8f726b0ab2915e6d5a58dea..39b3231850a95d3b9f0dbab51eaaafd2d7063f71 100644 (file)
@@ -273,3 +273,22 @@ func ExampleIndent() {
        // =    }
        // =]
 }
+
+func ExampleMarshalIndent() {
+       data := map[string]int{
+               "a": 1,
+               "b": 2,
+       }
+
+       json, err := json.MarshalIndent(data, "<prefix>", "<indent>")
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       fmt.Println(string(json))
+       // Output:
+       // {
+       // <prefix><indent>"a": 1,
+       // <prefix><indent>"b": 2
+       // <prefix>}
+}