1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

write object

This commit is contained in:
Tao Wen 2017-01-07 22:08:45 +08:00
parent 5e50b3e11c
commit 21549b9fd8
4 changed files with 113 additions and 6 deletions

View File

@ -422,3 +422,11 @@ func (stream *Stream) writeUint64SlowPath(val uint64) {
} }
stream.Write(temp[charPos:]) stream.Write(temp[charPos:])
} }
func (stream *Stream) WriteInt(val int) {
stream.WriteInt64(int64(val))
}
func (stream *Stream) WriteUint(val uint) {
stream.WriteUint64(uint64(val))
}

View File

@ -1,6 +1,10 @@
package jsoniter package jsoniter
import "testing" import (
"testing"
"bytes"
"github.com/json-iterator/go/require"
)
func Test_true(t *testing.T) { func Test_true(t *testing.T) {
iter := ParseString(`true`) iter := ParseString(`true`)
@ -15,3 +19,15 @@ func Test_false(t *testing.T) {
t.FailNow() t.FailNow()
} }
} }
func Test_write_true_false(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.WriteTrue()
stream.WriteFalse()
stream.Flush()
should.Nil(stream.Error)
should.Equal("truefalse", buf.String())
}

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"testing" "testing"
"github.com/json-iterator/go/require"
"bytes"
) )
func Test_empty_object(t *testing.T) { func Test_empty_object(t *testing.T) {
@ -66,6 +68,23 @@ func Test_two_field(t *testing.T) {
} }
} }
func Test_write_object(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.IndentionStep = 2
stream.WriteObjectStart()
stream.WriteObjectField("hello")
stream.WriteInt(1)
stream.WriteMore()
stream.WriteObjectField("world")
stream.WriteInt(2)
stream.WriteObjectEnd()
stream.Flush()
should.Nil(stream.Error)
should.Equal("{\n hello:1,\n world:2\n}", buf.String())
}
type TestObj struct { type TestObj struct {
Field1 string Field1 string
Field2 uint64 Field2 uint64

View File

@ -5,9 +5,13 @@ import (
) )
var bytesNull []byte var bytesNull []byte
var bytesTrue []byte
var bytesFalse []byte
func init() { func init() {
bytesNull = []byte("null") bytesNull = []byte("null")
bytesTrue = []byte("true")
bytesFalse = []byte("false")
} }
type Stream struct { type Stream struct {
@ -15,10 +19,12 @@ type Stream struct {
buf []byte buf []byte
n int n int
Error error Error error
indention int
IndentionStep int
} }
func NewStream(out io.Writer, bufSize int) *Stream { func NewStream(out io.Writer, bufSize int) *Stream {
return &Stream{out, make([]byte, bufSize), 0, nil} return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
} }
@ -116,5 +122,63 @@ func (stream *Stream) WriteNull() {
stream.Write(bytesNull) stream.Write(bytesNull)
} }
func (stream *Stream) WriteTrue() {
stream.Write(bytesTrue)
}
func (stream *Stream) WriteFalse() {
stream.Write(bytesFalse)
}
func (stream *Stream) WriteBool(val bool) {
if val {
stream.Write(bytesTrue)
} else {
stream.Write(bytesFalse)
}
}
func (stream *Stream) WriteObjectStart() {
stream.indention += stream.IndentionStep
stream.writeByte('{')
stream.writeIndention(0)
}
func (stream *Stream) WriteObjectField(field string) {
stream.WriteString(field)
stream.writeByte(':')
}
func (stream *Stream) WriteObjectEnd() {
stream.writeIndention(stream.IndentionStep)
stream.indention -= stream.IndentionStep
stream.writeByte('}')
}
func (stream *Stream) WriteMore() {
stream.writeByte(',')
stream.writeIndention(0)
}
func (stream *Stream) writeIndention(delta int) {
if (stream.indention == 0) {
return
}
stream.writeByte('\n')
toWrite := stream.indention - delta
i := 0
for {
for ; i < toWrite && stream.n < len(stream.buf); i++ {
stream.buf[stream.n] = ' '
stream.n ++
}
if i == toWrite {
break;
} else {
stream.Flush()
}
}
}
func (stream *Stream) WriteVal(val interface{}) { func (stream *Stream) WriteVal(val interface{}) {
} }