Skip to content

Commit

Permalink
doc: add streaming examples (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 authored May 19, 2023
1 parent d393a32 commit f1a9b94
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/example_stream_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f1a9b94

Please sign in to comment.