File: update_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 (68 lines) | stat: -rw-r--r-- 1,370 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package fastjson_test

import (
	"fmt"
	"log"

	"github.com/valyala/fastjson"
)

func ExampleObject_Del() {
	v := fastjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
	o, err := v.Object()
	if err != nil {
		log.Fatalf("cannot otain object: %s", err)
	}
	fmt.Printf("%s\n", o)

	o.Del("bar")
	fmt.Printf("%s\n", o)

	o.Del("foo")
	fmt.Printf("%s\n", o)

	o.Del("baz")
	fmt.Printf("%s\n", o)

	// Output:
	// {"foo":123,"bar":[1,2],"baz":"xyz"}
	// {"foo":123,"baz":"xyz"}
	// {"baz":"xyz"}
	// {}
}

func ExampleValue_Del() {
	v := fastjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
	fmt.Printf("%s\n", v)

	v.Del("foo")
	fmt.Printf("%s\n", v)

	v.Get("bar").Del("0")
	fmt.Printf("%s\n", v)

	// Output:
	// {"foo":123,"bar":[1,2],"baz":"xyz"}
	// {"bar":[1,2],"baz":"xyz"}
	// {"bar":[2],"baz":"xyz"}
}

func ExampleValue_Set() {
	v := fastjson.MustParse(`{"foo":1,"bar":[2,3]}`)

	// Replace `foo` value with "xyz"
	v.Set("foo", fastjson.MustParse(`"xyz"`))
	// Add "newv":123
	v.Set("newv", fastjson.MustParse(`123`))
	fmt.Printf("%s\n", v)

	// Replace `bar.1` with {"x":"y"}
	v.Get("bar").Set("1", fastjson.MustParse(`{"x":"y"}`))
	// Add `bar.3="qwe"
	v.Get("bar").Set("3", fastjson.MustParse(`"qwe"`))
	fmt.Printf("%s\n", v)

	// Output:
	// {"foo":"xyz","bar":[2,3],"newv":123}
	// {"foo":"xyz","bar":[2,{"x":"y"},null,"qwe"],"newv":123}
}