1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-18 22:57:33 +02:00

add stream

This commit is contained in:
Tao Wen
2017-01-07 12:28:16 +08:00
parent 5af8cc4b09
commit 6f57d41461
8 changed files with 394 additions and 43 deletions

View File

@ -4,9 +4,10 @@ import (
"bytes"
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
)
func Test_string_empty(t *testing.T) {
func Test_decode_string_empty(t *testing.T) {
iter := Parse(bytes.NewBufferString(`""`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -17,7 +18,7 @@ func Test_string_empty(t *testing.T) {
}
}
func Test_string_hello(t *testing.T) {
func Test_decode_string_hello(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"hello"`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -28,7 +29,7 @@ func Test_string_hello(t *testing.T) {
}
}
func Test_string_escape_quote(t *testing.T) {
func Test_decode_string_escape_quote(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"hel\"lo"`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -39,7 +40,7 @@ func Test_string_escape_quote(t *testing.T) {
}
}
func Test_string_escape_newline(t *testing.T) {
func Test_decode_string_escape_newline(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"hel\nlo"`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -50,7 +51,7 @@ func Test_string_escape_newline(t *testing.T) {
}
}
func Test_string_escape_unicode(t *testing.T) {
func Test_decode_string_escape_unicode(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"\u4e2d\u6587"`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -61,7 +62,7 @@ func Test_string_escape_unicode(t *testing.T) {
}
}
func Test_string_escape_unicode_with_surrogate(t *testing.T) {
func Test_decode_string_escape_unicode_with_surrogate(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"\ud83d\udc4a"`), 4096)
val := iter.ReadString()
if iter.Error != nil {
@ -72,7 +73,7 @@ func Test_string_escape_unicode_with_surrogate(t *testing.T) {
}
}
func Test_string_as_bytes(t *testing.T) {
func Test_decode_string_as_bytes(t *testing.T) {
iter := Parse(bytes.NewBufferString(`"hello""world"`), 4096)
val := string(iter.readStringAsBytes())
if val != "hello" {
@ -84,6 +85,16 @@ func Test_string_as_bytes(t *testing.T) {
}
}
func Test_write_string(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.WriteString("hello")
stream.Flush()
should.Nil(stream.Error)
should.Equal("hello", buf.String())
}
func Benchmark_jsoniter_unicode(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(`"\ud83d\udc4a"`)