forked from go-faster/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
197 lines (171 loc) · 3.64 KB
/
examples_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package jx_test
import (
"fmt"
"github.com/go-faster/jx"
)
func ExampleDecodeStr() {
d := jx.DecodeStr(`{"values":[4,8,15,16,23,42]}`)
// Save all integers from "values" array to slice.
var values []int
// Iterate over each object field.
if err := d.Obj(func(d *jx.Decoder, key string) error {
switch key {
case "values":
// Iterate over each array element.
return d.Arr(func(d *jx.Decoder) error {
v, err := d.Int()
if err != nil {
return err
}
values = append(values, v)
return nil
})
default:
// Skip unknown fields if any.
return d.Skip()
}
}); err != nil {
panic(err)
}
fmt.Println(values)
// Output: [4 8 15 16 23 42]
}
func ExampleEncoder_String() {
var e jx.Encoder
e.ObjStart() // {
e.FieldStart("values") // "values":
e.ArrStart() // [
for _, v := range []int{4, 8, 15, 16, 23, 42} {
e.Int(v)
}
e.ArrEnd() // ]
e.ObjEnd() // }
fmt.Println(e)
fmt.Println("Buffer len:", len(e.Bytes()))
// Output: {"values":[4,8,15,16,23,42]}
// Buffer len: 28
}
func ExampleValid() {
fmt.Println(jx.Valid([]byte(`{"field": "value"}`)))
fmt.Println(jx.Valid([]byte(`"Hello, world!"`)))
fmt.Println(jx.Valid([]byte(`["foo"}`)))
// Output: true
// true
// false
}
func ExampleDecoder_Capture() {
d := jx.DecodeStr(`["foo", "bar", "baz"]`)
var elems int
// NB: Currently Capture does not support io.Reader, only buffers.
if err := d.Capture(func(d *jx.Decoder) error {
// Everything decoded in this callback will be rolled back.
return d.Arr(func(d *jx.Decoder) error {
elems++
return d.Skip()
})
}); err != nil {
panic(err)
}
// Decoder is rolled back to state before "Capture" call.
fmt.Println("Read", elems, "elements on first pass")
fmt.Println("Next element is", d.Next(), "again")
// Output:
// Read 3 elements on first pass
// Next element is array again
}
func ExampleDecoder_Raw() {
d := jx.DecodeStr(`{"foo": [1, 2, 3]}`)
var raw jx.Raw
if err := d.Obj(func(d *jx.Decoder, key string) error {
v, err := d.Raw()
if err != nil {
return err
}
raw = v
return nil
}); err != nil {
panic(err)
}
fmt.Println(raw.Type(), raw)
// Output:
// array [1, 2, 3]
}
func ExampleDecoder_Num() {
// Can decode numbers and number strings.
d := jx.DecodeStr(`{"foo": "10531.0"}`)
var n jx.Num
if err := d.Obj(func(d *jx.Decoder, key string) error {
v, err := d.Num()
if err != nil {
return err
}
n = v
return nil
}); err != nil {
panic(err)
}
fmt.Println(n)
fmt.Println("positive:", n.Positive())
// Can decode floats with zero fractional part as integers:
v, err := n.Int64()
if err != nil {
panic(err)
}
fmt.Println("int64:", v)
// Output:
// "10531.0"
// positive: true
// int64: 10531
}
func ExampleEncoder_Base64() {
var e jx.Encoder
e.Base64([]byte("Hello"))
fmt.Println(e)
data, _ := jx.DecodeBytes(e.Bytes()).Base64()
fmt.Printf("%s", data)
// Output:
// "SGVsbG8="
// Hello
}
func ExampleDecoder_Base64() {
data, _ := jx.DecodeStr(`"SGVsbG8="`).Base64()
fmt.Printf("%s", data)
// Output:
// Hello
}
func Example() {
var e jx.Encoder
e.Obj(func(e *jx.Encoder) {
e.FieldStart("data")
e.Base64([]byte("hello"))
})
fmt.Println(e)
if err := jx.DecodeBytes(e.Bytes()).Obj(func(d *jx.Decoder, key string) error {
v, err := d.Base64()
fmt.Printf("%s: %s\n", key, v)
return err
}); err != nil {
panic(err)
}
// Output: {"data":"aGVsbG8="}
// data: hello
}
func ExampleEncoder_SetIdent() {
var e jx.Encoder
e.SetIdent(2)
e.ObjStart()
e.FieldStart("data")
e.ArrStart()
e.Int(1)
e.Int(2)
e.ArrEnd()
e.ObjEnd()
fmt.Println(e)
// Output:
// {
// "data": [
// 1,
// 2
// ]
// }
}