File: handy_example_test.go

package info (click to toggle)
golang-github-valyala-fastjson 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,828 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,249 bytes parent folder | download | duplicates (3)
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
package fastjson_test

import (
	"fmt"
	"github.com/valyala/fastjson"
)

func ExampleGetString() {
	data := []byte(`{"foo":{"bar":[123,"baz"]}}`)

	s := fastjson.GetString(data, "foo", "bar", "1")
	fmt.Printf("data.foo.bar[1] = %s", s)

	// Output:
	// data.foo.bar[1] = baz
}

func ExampleGetInt() {
	data := []byte(`{"foo": [233,true, {"bar": [2343]} ]}`)

	n1 := fastjson.GetInt(data, "foo", "0")
	fmt.Printf("data.foo[0] = %d\n", n1)

	n2 := fastjson.GetInt(data, "foo", "2", "bar", "0")
	fmt.Printf("data.foo[2].bar[0] = %d\n", n2)

	// Output:
	// data.foo[0] = 233
	// data.foo[2].bar[0] = 2343
}

func ExampleExists() {
	data := []byte(`{"foo": [1.23,{"bar":33,"baz":null}]}`)

	fmt.Printf("exists(data.foo) = %v\n", fastjson.Exists(data, "foo"))
	fmt.Printf("exists(data.foo[0]) = %v\n", fastjson.Exists(data, "foo", "0"))
	fmt.Printf("exists(data.foo[1].baz) = %v\n", fastjson.Exists(data, "foo", "1", "baz"))
	fmt.Printf("exists(data.foobar) = %v\n", fastjson.Exists(data, "foobar"))
	fmt.Printf("exists(data.foo.bar) = %v\n", fastjson.Exists(data, "foo", "bar"))

	// Output:
	// exists(data.foo) = true
	// exists(data.foo[0]) = true
	// exists(data.foo[1].baz) = true
	// exists(data.foobar) = false
	// exists(data.foo.bar) = false
}