gitee.com/quant1x/pkg@v0.2.8/fastjson/scanner_example_test.go (about)

     1  package fastjson_test
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/pkg/fastjson"
     6  	"log"
     7  )
     8  
     9  func ExampleScanner() {
    10  	var sc fastjson.Scanner
    11  
    12  	sc.Init(`   {"foo":  "bar"  }[  ]
    13  		12345"xyz" true false null    `)
    14  
    15  	for sc.Next() {
    16  		fmt.Printf("%s\n", sc.Value())
    17  	}
    18  	if err := sc.Error(); err != nil {
    19  		log.Fatalf("unexpected error: %s", err)
    20  	}
    21  
    22  	// Output:
    23  	// {"foo":"bar"}
    24  	// []
    25  	// 12345
    26  	// "xyz"
    27  	// true
    28  	// false
    29  	// null
    30  }
    31  
    32  func ExampleScanner_reuse() {
    33  	var sc fastjson.Scanner
    34  
    35  	// The sc may be re-used in order to reduce the number
    36  	// of memory allocations.
    37  	for i := 0; i < 3; i++ {
    38  		s := fmt.Sprintf(`[%d] "%d"`, i, i)
    39  		sc.Init(s)
    40  		for sc.Next() {
    41  			fmt.Printf("%s,", sc.Value())
    42  		}
    43  		if err := sc.Error(); err != nil {
    44  			log.Fatalf("unexpected error: %s", err)
    45  		}
    46  		fmt.Printf("\n")
    47  	}
    48  
    49  	// Output:
    50  	// [0],"0",
    51  	// [1],"1",
    52  	// [2],"2",
    53  }