-
Notifications
You must be signed in to change notification settings - Fork 21
/
json.go
88 lines (78 loc) · 1.64 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// https://www.socketloop.com/tutorials/golang-http-response-json-encoded-data
func main() {
fmt.Println("=== json ===")
marshal()
unmarshal()
encode()
decode()
}
// marshal converts a JSON string to objects
func marshal() {
fmt.Println("=== json.marshal ===")
ryan := &Person{"Ryan", 25}
wire, err := json.Marshal(ryan)
check(err)
fmt.Println(string(wire))
}
// unmarshal converts an object to a JSON string
func unmarshal() {
fmt.Println("=== json.unmarshal ===")
var jsonBlob = []byte(`[
{"name": "Bill", "age": 109},
{"name": "Bob", "age": 5}
]`)
var persons []Person
err := json.Unmarshal(jsonBlob, &persons)
check(err)
fmt.Printf("%+v\n", persons)
}
// encode writes objects to a JSON *stream*
func encode() {
fmt.Println("=== json.encode ===")
f, err := os.Create("deleteme.json")
check(err)
enc := json.NewEncoder(f)
// or enc := json.NewEncoder(os.Stdout)
p := Person{Name: "Joe", Age: 2}
err = enc.Encode(&p)
check(err)
fmt.Printf("%s: %d\n", p.Name, p.Age)
os.Remove("deleteme.json")
}
// decode reads a JSON *stream* into objects
func decode() {
fmt.Println("=== json.decode ===")
const jsonStream = `
{"name": "Ed", "age": 55}
{"name": "Ethan", "age": 33}
{"name": "Elbert", "age": 111}
`
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
var m Person
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %d\n", m.Name, m.Age)
}
}
func check(e error) {
if e != nil {
panic(e)
}
}