github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/examples/example_stream_test.go (about) 1 package example 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "github.com/bytedance/sonic" 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 23 // This example uses a Encoder to encode streamingly. 24 func ExampleStreamEncoder() { 25 var o1 = map[string]interface{}{ 26 "a": "b", 27 } 28 var o2 = 1 29 var w = bytes.NewBuffer(nil) 30 var enc = sonic.ConfigDefault.NewEncoder(w) 31 enc.Encode(o1) 32 enc.Encode(o2) 33 fmt.Println(w.String()) 34 // Output: 35 // {"a":"b"} 36 // 1 37 }