-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
270 lines (249 loc) · 5.2 KB
/
Copy pathexample_test.go
File metadata and controls
270 lines (249 loc) · 5.2 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package jsonc_test
import (
"bytes"
"fmt"
"strings"
"github.com/go-rotini/jsonc"
)
func ExampleUnmarshal() {
src := []byte(`{
// Server configuration
"port": 8080,
"host": "localhost",
}`)
type Config struct {
Port int `json:"port"`
Host string `json:"host"`
}
var cfg Config
if err := jsonc.Unmarshal(src, &cfg); err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("%s:%d\n", cfg.Host, cfg.Port)
// Output: localhost:8080
}
func ExampleUnmarshalTo() {
src := []byte(`{"name": "alice", "age": 30}`)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
p, err := jsonc.UnmarshalTo[Person](src)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(p.Name, p.Age)
// Output: alice 30
}
func ExampleMarshal() {
type Config struct {
Port int `json:"port"`
Host string `json:"host"`
}
out, err := jsonc.Marshal(Config{Port: 8080, Host: "localhost"})
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(out))
// Output: {"port": 8080, "host": "localhost"}
}
func ExampleMarshalIndent() {
out, err := jsonc.MarshalIndent(map[string]int{"a": 1, "b": 2}, " ")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(out))
// Output:
// {
// "a": 1,
// "b": 2
// }
}
func ExampleToJSON() {
src := []byte(`{
// kept as standard JSON output
"port": 8080,
"hosts": ["a", "b",], // trailing comma
}`)
out, err := jsonc.ToJSON(src)
if err != nil {
fmt.Println("error:", err)
return
}
// Output is valid RFC 8259 JSON.
fmt.Println(strings.Contains(string(out), "//"))
fmt.Println(strings.Contains(string(out), ",]"))
// Output:
// false
// false
}
func ExampleFormat() {
src := []byte(`{"a":1,"b":[1,2,3]}`)
out, err := jsonc.Format(src)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(out))
// Output:
// {
// "a": 1,
// "b": [
// 1,
// 2,
// 3
// ]
// }
}
func ExampleMinimize() {
src := []byte(`{
"a": 1,
"b": 2
}`)
out, err := jsonc.Minimize(src)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(out))
// Output: {"a": 1, "b": 2}
}
func ExamplePathString() {
src := []byte(`{
"server": {
"port": 8080,
"hosts": ["a.example", "b.example"]
}
}`)
f, err := jsonc.Parse(src)
if err != nil {
fmt.Println("error:", err)
return
}
p, _ := jsonc.PathString("$.server.hosts[1]")
host, err := p.ReadString(f.Root)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(host)
// Output: b.example
}
func ExamplePathPointer() {
src := []byte(`{"users": [{"name": "alice"}, {"name": "bob"}]}`)
f, _ := jsonc.Parse(src)
p, _ := jsonc.PathPointer("/users/1/name")
name, _ := p.ReadString(f.Root)
fmt.Println(name)
// Output: bob
}
func ExamplePatch_Apply() {
doc := []byte(`{"a": 1, "b": 2}`)
patch := []byte(`[
{"op": "replace", "path": "/a", "value": 100},
{"op": "add", "path": "/c", "value": 3}
]`)
f, _ := jsonc.Parse(doc)
p, _ := jsonc.ParsePatch(patch)
out, err := p.Apply(f.Root)
if err != nil {
fmt.Println("error:", err)
return
}
bytes, _ := jsonc.NodeToBytes(out)
fmt.Println(string(bytes))
// Output: {"a": 100, "b": 2, "c": 3}
}
func ExampleEncoder() {
var buf bytes.Buffer
enc := jsonc.NewEncoder(&buf, jsonc.WithIndent(" "))
for _, v := range []map[string]int{{"a": 1}, {"b": 2}} {
if err := enc.Encode(v); err != nil {
fmt.Println("error:", err)
return
}
}
fmt.Print(buf.String())
// Output:
// {
// "a": 1
// }
// {
// "b": 2
// }
}
func ExampleDecoder() {
src := strings.NewReader(`1 2 3`)
dec := jsonc.NewDecoder(src)
var sum int
for dec.More() {
var n int
if err := dec.Decode(&n); err != nil {
fmt.Println("error:", err)
return
}
sum += n
}
fmt.Println(sum)
// Output: 6
}
func ExampleRawValue() {
type Wrapper struct {
Meta jsonc.RawValue `json:"meta"`
Name string `json:"name"`
}
src := []byte(`{"meta": {"k": 1, /* preserved */ "v": [1,2,3]}, "name": "x"}`)
var w Wrapper
if err := jsonc.Unmarshal(src, &w); err != nil {
fmt.Println("error:", err)
return
}
// RawValue keeps the source bytes verbatim, including comments.
fmt.Println(strings.Contains(string(w.Meta), "preserved"))
// Output: true
}
func ExampleWithComment() {
type Server struct {
Port int `json:"port"`
Host string `json:"host"`
}
s := Server{Port: 8080, Host: "localhost"}
out, err := jsonc.MarshalWithOptions(s,
jsonc.WithIndent(" "),
jsonc.WithComment(map[string][]jsonc.Comment{
"port": {
{Position: jsonc.HeadCommentPos, Text: "Listening port"},
{Position: jsonc.LineCommentPos, Text: "default"},
},
"host": {
{Position: jsonc.FootCommentPos, Text: "Host configuration ends here."},
},
}),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(out))
// Output:
// {
// // Listening port
// "port": 8080, // default
// "host": "localhost"
// // Host configuration ends here.
// }
}
func ExampleStripComments() {
src := []byte(`{"a": 1 /* drop me */, "b": 2 // and me
}`)
out, _ := jsonc.StripComments(src)
fmt.Println(strings.Contains(string(out), "/*"))
fmt.Println(strings.Contains(string(out), "//"))
// Output:
// false
// false
}