-
Notifications
You must be signed in to change notification settings - Fork 114
/
decode_example_test.go
150 lines (125 loc) · 2.53 KB
/
decode_example_test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gojay_test
import (
"fmt"
"log"
"strings"
"github.com/francoispqt/gojay"
)
func ExampleUnmarshal_string() {
data := []byte(`"gojay"`)
var str string
err := gojay.Unmarshal(data, &str)
if err != nil {
log.Fatal(err)
}
fmt.Println(str) // true
}
func ExampleUnmarshal_bool() {
data := []byte(`true`)
var b bool
err := gojay.Unmarshal(data, &b)
if err != nil {
log.Fatal(err)
}
fmt.Println(b) // true
}
func ExampleUnmarshal_invalidType() {
data := []byte(`"gojay"`)
someStruct := struct{}{}
err := gojay.Unmarshal(data, &someStruct)
fmt.Println(err) // "Cannot unmarshal JSON to type '*struct{}'"
}
func ExampleDecoder_Decode_string() {
var str string
dec := gojay.BorrowDecoder(strings.NewReader(`"gojay"`))
err := dec.Decode(&str)
dec.Release()
if err != nil {
log.Fatal(err)
}
fmt.Println(str) // "gojay"
}
func ExampleDecodeObjectFunc() {
reader := strings.NewReader(`{
"name": "John Doe",
"email": "[email protected]"
}`)
dec := gojay.NewDecoder(reader)
user := struct {
name string
email string
}{}
dec.DecodeObject(gojay.DecodeObjectFunc(func(dec *gojay.Decoder, k string) error {
switch k {
case "name":
return dec.String(&user.name)
case "email":
return dec.String(&user.email)
}
return nil
}))
fmt.Printf("User\nname: %s\nemail: %s", user.name, user.email)
// Output:
// User
// name: John Doe
// email: [email protected]
}
func ExampleDecodeArrayFunc() {
reader := strings.NewReader(`[
"foo",
"bar"
]`)
dec := gojay.NewDecoder(reader)
strSlice := make([]string, 0)
err := dec.DecodeArray(gojay.DecodeArrayFunc(func(dec *gojay.Decoder) error {
var str string
if err := dec.AddString(&str); err != nil {
return err
}
strSlice = append(strSlice, str)
return nil
}))
if err != nil {
log.Fatal(err)
}
fmt.Print(strSlice)
// Output:
// [foo bar]
}
func ExampleNewDecoder() {
reader := strings.NewReader(`"gojay"`)
dec := gojay.NewDecoder(reader)
var str string
err := dec.DecodeString(&str)
if err != nil {
log.Fatal(err)
}
fmt.Println(str)
// Output:
// gojay
}
func ExampleBorrowDecoder() {
reader := strings.NewReader(`"gojay"`)
dec := gojay.BorrowDecoder(reader)
defer dec.Release()
var str string
err := dec.DecodeString(&str)
if err != nil {
log.Fatal(err)
}
fmt.Println(str)
// Output:
// gojay
}
func ExampleDecoder_DecodeBool() {
reader := strings.NewReader(`true`)
dec := gojay.NewDecoder(reader)
var b bool
err := dec.DecodeBool(&b)
if err != nil {
log.Fatal(err)
}
fmt.Println(b)
// Output:
// true
}