Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1a9b94

Browse files
authoredMay 19, 2023
doc: add streaming examples (bytedance#422)
1 parent d393a32 commit f1a9b94

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎examples/example_stream_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.