LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json_test | 5 package json_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // YCbCr &{255 0 -10} | 128 // YCbCr &{255 0 -10} |
129 // RGB &{98 218 255} | 129 // RGB &{98 218 255} |
130 } | 130 } |
131 | 131 |
132 func ExampleIndent() { | 132 func ExampleIndent() { |
133 type Road struct { | 133 type Road struct { |
134 Name string | 134 Name string |
135 Number int | 135 Number int |
136 } | 136 } |
137 roads := []Road{ | 137 roads := []Road{ |
138 » » Road{"Diamond Fork", 29}, | 138 » » {"Diamond Fork", 29}, |
139 » » Road{"Sheep Creek", 51}, | 139 » » {"Sheep Creek", 51}, |
140 } | 140 } |
141 | 141 |
142 b, err := json.Marshal(roads) | 142 b, err := json.Marshal(roads) |
143 if err != nil { | 143 if err != nil { |
144 log.Fatal(err) | 144 log.Fatal(err) |
145 } | 145 } |
146 | 146 |
147 var out bytes.Buffer | 147 var out bytes.Buffer |
148 » out.Write([]byte("ʕ◔ϖ◔ʔ{\nʕ◔ϖ◔ʔ\t\"destinations\": ")) | 148 » json.Indent(&out, b, "=", "\t") |
149 » // n.b. the previous write did not end with newline, and the following d
oes not start with the "ʕ◔ϖ◔ʔ" | |
150 » json.Indent(&out, b, "ʕ◔ϖ◔ʔ\t", "\t") | |
151 » out.Write([]byte("\nʕ◔ϖ◔ʔ}\n")) | |
152 | |
153 out.WriteTo(os.Stdout) | 149 out.WriteTo(os.Stdout) |
154 // Output: | 150 // Output: |
155 » // ʕ◔ϖ◔ʔ{ | 151 » // [ |
156 » // ʕ◔ϖ◔ʔ» "destinations": [ | 152 » // = { |
157 » // ʕ◔ϖ◔ʔ» » { | 153 » // = "Name": "Diamond Fork", |
158 » // ʕ◔ϖ◔ʔ» » » "Name": "Diamond Fork", | 154 » // = "Number": 29 |
159 » // ʕ◔ϖ◔ʔ» » » "Number": 29 | 155 » // = }, |
160 » // ʕ◔ϖ◔ʔ» » }, | 156 » // = { |
161 » // ʕ◔ϖ◔ʔ» » { | 157 » // = "Name": "Sheep Creek", |
162 » // ʕ◔ϖ◔ʔ» » » "Name": "Sheep Creek", | 158 » // = "Number": 51 |
163 » // ʕ◔ϖ◔ʔ» » » "Number": 51 | 159 » // = } |
164 » // ʕ◔ϖ◔ʔ» » } | 160 » // =] |
165 » // ʕ◔ϖ◔ʔ» ] | |
166 » // ʕ◔ϖ◔ʔ} | |
167 } | 161 } |
LEFT | RIGHT |