mirror of
https://github.com/ii64/sonic.git
synced 2026-06-20 16:45:22 +08:00
doc: add streaming examples (#422)
This commit is contained in:
parent
d393a3228d
commit
f1a9b94fea
1 changed files with 37 additions and 0 deletions
37
examples/example_stream_test.go
Normal file
37
examples/example_stream_test.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package example
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
// This example uses a Decoder to decode a stream of distinct JSON values.
|
||||
func ExampleStreamDecoder() {
|
||||
var o = map[string]interface{}{}
|
||||
var r = strings.NewReader(`{"a":"b"}{"1":"2"}`)
|
||||
var dec = sonic.ConfigDefault.NewDecoder(r)
|
||||
dec.Decode(&o)
|
||||
dec.Decode(&o)
|
||||
fmt.Printf("%+v", o)
|
||||
// Output:
|
||||
// map[1:2 a:b]
|
||||
}
|
||||
|
||||
|
||||
// This example uses a Encoder to encode streamingly.
|
||||
func ExampleStreamEncoder() {
|
||||
var o1 = map[string]interface{}{
|
||||
"a": "b",
|
||||
}
|
||||
var o2 = 1
|
||||
var w = bytes.NewBuffer(nil)
|
||||
var enc = sonic.ConfigDefault.NewEncoder(w)
|
||||
enc.Encode(o1)
|
||||
enc.Encode(o2)
|
||||
fmt.Println(w.String())
|
||||
// Output:
|
||||
// {"a":"b"}
|
||||
// 1
|
||||
}
|
||||
Loading…
Reference in a new issue