mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
write array
This commit is contained in:
parent
21549b9fd8
commit
e034897e69
@ -3,6 +3,8 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_empty_array(t *testing.T) {
|
func Test_empty_array(t *testing.T) {
|
||||||
@ -117,6 +119,21 @@ func Test_whitespace_before_comma(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_write_array(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
stream := NewStream(buf, 4096)
|
||||||
|
stream.IndentionStep = 2
|
||||||
|
stream.WriteArrayStart()
|
||||||
|
stream.WriteInt(1)
|
||||||
|
stream.WriteMore()
|
||||||
|
stream.WriteInt(2)
|
||||||
|
stream.WriteArrayEnd()
|
||||||
|
stream.Flush()
|
||||||
|
should.Nil(stream.Error)
|
||||||
|
should.Equal("[\n 1,\n 2\n]", buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_array(b *testing.B) {
|
func Benchmark_jsoniter_array(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
||||||
|
12
stream.go
12
stream.go
@ -160,6 +160,18 @@ func (stream *Stream) WriteMore() {
|
|||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteArrayStart() {
|
||||||
|
stream.indention += stream.IndentionStep
|
||||||
|
stream.writeByte('[')
|
||||||
|
stream.writeIndention(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteArrayEnd() {
|
||||||
|
stream.writeIndention(stream.IndentionStep)
|
||||||
|
stream.indention -= stream.IndentionStep
|
||||||
|
stream.writeByte(']')
|
||||||
|
}
|
||||||
|
|
||||||
func (stream *Stream) writeIndention(delta int) {
|
func (stream *Stream) writeIndention(delta int) {
|
||||||
if (stream.indention == 0) {
|
if (stream.indention == 0) {
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user