mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
write object
This commit is contained in:
parent
5e50b3e11c
commit
21549b9fd8
@ -421,4 +421,12 @@ func (stream *Stream) writeUint64SlowPath(val uint64) {
|
||||
}
|
||||
}
|
||||
stream.Write(temp[charPos:])
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteInt(val int) {
|
||||
stream.WriteInt64(int64(val))
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteUint(val uint) {
|
||||
stream.WriteUint64(uint64(val))
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"github.com/json-iterator/go/require"
|
||||
)
|
||||
|
||||
func Test_true(t *testing.T) {
|
||||
iter := ParseString(`true`)
|
||||
@ -15,3 +19,15 @@ func Test_false(t *testing.T) {
|
||||
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())
|
||||
}
|
@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
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 {
|
||||
Field1 string
|
||||
Field2 uint64
|
||||
|
74
stream.go
74
stream.go
@ -5,20 +5,26 @@ import (
|
||||
)
|
||||
|
||||
var bytesNull []byte
|
||||
var bytesTrue []byte
|
||||
var bytesFalse []byte
|
||||
|
||||
func init() {
|
||||
bytesNull = []byte("null")
|
||||
bytesTrue = []byte("true")
|
||||
bytesFalse = []byte("false")
|
||||
}
|
||||
|
||||
type Stream struct {
|
||||
out io.Writer
|
||||
buf []byte
|
||||
n int
|
||||
Error error
|
||||
out io.Writer
|
||||
buf []byte
|
||||
n int
|
||||
Error error
|
||||
indention int
|
||||
IndentionStep int
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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{}) {
|
||||
}
|
Loading…
Reference in New Issue
Block a user