File: scanner_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 (53 lines) | stat: -rw-r--r-- 855 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
package fastjson_test

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

func ExampleScanner() {
	var sc fastjson.Scanner

	sc.Init(`   {"foo":  "bar"  }[  ]
		12345"xyz" true false null    `)

	for sc.Next() {
		fmt.Printf("%s\n", sc.Value())
	}
	if err := sc.Error(); err != nil {
		log.Fatalf("unexpected error: %s", err)
	}

	// Output:
	// {"foo":"bar"}
	// []
	// 12345
	// "xyz"
	// true
	// false
	// null
}

func ExampleScanner_reuse() {
	var sc fastjson.Scanner

	// The sc may be re-used in order to reduce the number
	// of memory allocations.
	for i := 0; i < 3; i++ {
		s := fmt.Sprintf(`[%d] "%d"`, i, i)
		sc.Init(s)
		for sc.Next() {
			fmt.Printf("%s,", sc.Value())
		}
		if err := sc.Error(); err != nil {
			log.Fatalf("unexpected error: %s", err)
		}
		fmt.Printf("\n")
	}

	// Output:
	// [0],"0",
	// [1],"1",
	// [2],"2",
}