소스 검색

write object

Tao Wen 9 년 전
부모
커밋
21549b9fd8
4개의 변경된 파일113개의 추가작업 그리고 6개의 파일을 삭제
  1. 8 0
      feature_stream_int.go
  2. 17 1
      jsoniter_bool_test.go
  3. 19 0
      jsoniter_object_test.go
  4. 69 5
      stream.go

+ 8 - 0
feature_stream_int.go

@@ -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))
 }

+ 17 - 1
jsoniter_bool_test.go

@@ -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())
+}

+ 19 - 0
jsoniter_object_test.go

@@ -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

+ 69 - 5
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{}) {
 }