diff --git a/examples/example_stream_test.go b/examples/example_stream_test.go new file mode 100644 index 000000000..96a644c3c --- /dev/null +++ b/examples/example_stream_test.go @@ -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 +} \ No newline at end of file