-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
55 lines (46 loc) · 868 Bytes
/
Copy pathexample_test.go
File metadata and controls
55 lines (46 loc) · 868 Bytes
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
package libjson_test
import (
"fmt"
"os"
"github.com/xnacly/libjson"
)
func ExampleNew() {
input := `{ "hello": {"world": ["hi"] } }`
doc, err := libjson.New([]byte(input))
if err != nil {
panic(err)
}
value, err := libjson.Get[string](&doc, ".hello.world.0")
if err != nil {
panic(err)
}
fmt.Println(value)
// Output: hi
}
func ExampleFromFile() {
f, err := os.CreateTemp("", "libjson-example-*.json")
if err != nil {
panic(err)
}
defer os.Remove(f.Name())
_, err = f.WriteString(`{ "hello": {"world": ["hi"] } }`)
if err != nil {
panic(err)
}
_, err = f.Seek(0, 0)
if err != nil {
panic(err)
}
doc, err := libjson.FromFile(f)
if err != nil {
panic(err)
}
defer doc.Close()
defer f.Close()
value, err := libjson.Get[string](&doc, ".hello.world.0")
if err != nil {
panic(err)
}
fmt.Println(value)
// Output: hi
}