OLD | NEW |
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 "encoding/json" | 9 "encoding/json" |
9 "fmt" | 10 "fmt" |
10 "io" | 11 "io" |
11 "log" | 12 "log" |
12 "os" | 13 "os" |
13 "strings" | 14 "strings" |
14 ) | 15 ) |
15 | 16 |
16 func ExampleMarshal() { | 17 func ExampleMarshal() { |
17 type ColorGroup struct { | 18 type ColorGroup struct { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 err := json.Unmarshal(c.Point, dst) | 121 err := json.Unmarshal(c.Point, dst) |
121 if err != nil { | 122 if err != nil { |
122 log.Fatalln("error:", err) | 123 log.Fatalln("error:", err) |
123 } | 124 } |
124 fmt.Println(c.Space, dst) | 125 fmt.Println(c.Space, dst) |
125 } | 126 } |
126 // Output: | 127 // Output: |
127 // YCbCr &{255 0 -10} | 128 // YCbCr &{255 0 -10} |
128 // RGB &{98 218 255} | 129 // RGB &{98 218 255} |
129 } | 130 } |
| 131 |
| 132 func ExampleIndent() { |
| 133 type Road struct { |
| 134 Name string |
| 135 Number int |
| 136 } |
| 137 roads := []Road{ |
| 138 {"Diamond Fork", 29}, |
| 139 {"Sheep Creek", 51}, |
| 140 } |
| 141 |
| 142 b, err := json.Marshal(roads) |
| 143 if err != nil { |
| 144 log.Fatal(err) |
| 145 } |
| 146 |
| 147 var out bytes.Buffer |
| 148 json.Indent(&out, b, "=", "\t") |
| 149 out.WriteTo(os.Stdout) |
| 150 // Output: |
| 151 // [ |
| 152 // = { |
| 153 // = "Name": "Diamond Fork", |
| 154 // = "Number": 29 |
| 155 // = }, |
| 156 // = { |
| 157 // = "Name": "Sheep Creek", |
| 158 // = "Number": 51 |
| 159 // = } |
| 160 // =] |
| 161 } |
OLD | NEW |