github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/examples/example_stream_test.go (about)

     1  package example
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/goshafaq/sonic"
     7  	"strings"
     8  )
     9  
    10  // This example uses a Decoder to decode a stream of distinct JSON values.
    11  func ExampleStreamDecoder() {
    12  	var o = map[string]interface{}{}
    13  	var r = strings.NewReader(`{"a":"b"}{"1":"2"}`)
    14  	var dec = sonic.ConfigDefault.NewDecoder(r)
    15  	dec.Decode(&o)
    16  	dec.Decode(&o)
    17  	fmt.Printf("%+v", o)
    18  	// Output:
    19  	// map[1:2 a:b]
    20  }
    21  
    22  // This example uses a Encoder to encode streamingly.
    23  func ExampleStreamEncoder() {
    24  	var o1 = map[string]interface{}{
    25  		"a": "b",
    26  	}
    27  	var o2 = 1
    28  	var w = bytes.NewBuffer(nil)
    29  	var enc = sonic.ConfigDefault.NewEncoder(w)
    30  	enc.Encode(o1)
    31  	enc.Encode(o2)
    32  	fmt.Println(w.String())
    33  	// Output:
    34  	// {"a":"b"}
    35  	// 1
    36  }