7 // stream is a io.Writer like object, with JSON specific write functions.
8 // Error is not returned as return value, but stored as Error member on this stream instance.
15 Attachment interface{} // open for customized encoder
18 // NewStream create new stream instance.
19 // cfg can be jsoniter.ConfigDefault.
20 // out can be nil if write to internal buffer.
21 // bufSize is the initial size for the internal buffer in bytes.
22 func NewStream(cfg API, out io.Writer, bufSize int) *Stream {
24 cfg: cfg.(*frozenConfig),
26 buf: make([]byte, 0, bufSize),
32 // Pool returns a pool can provide more stream with same configuration
33 func (stream *Stream) Pool() StreamPool {
37 // Reset reuse this stream instance by assign a new writer
38 func (stream *Stream) Reset(out io.Writer) {
40 stream.buf = stream.buf[:0]
43 // Available returns how many bytes are unused in the buffer.
44 func (stream *Stream) Available() int {
45 return cap(stream.buf) - len(stream.buf)
48 // Buffered returns the number of bytes that have been written into the current buffer.
49 func (stream *Stream) Buffered() int {
50 return len(stream.buf)
53 // Buffer if writer is nil, use this method to take the result
54 func (stream *Stream) Buffer() []byte {
58 // SetBuffer allows to append to the internal buffer directly
59 func (stream *Stream) SetBuffer(buf []byte) {
63 // Write writes the contents of p into the buffer.
64 // It returns the number of bytes written.
65 // If nn < len(p), it also returns an error explaining
66 // why the write is short.
67 func (stream *Stream) Write(p []byte) (nn int, err error) {
68 stream.buf = append(stream.buf, p...)
69 if stream.out != nil {
70 nn, err = stream.out.Write(stream.buf)
71 stream.buf = stream.buf[nn:]
77 // WriteByte writes a single byte.
78 func (stream *Stream) writeByte(c byte) {
79 stream.buf = append(stream.buf, c)
82 func (stream *Stream) writeTwoBytes(c1 byte, c2 byte) {
83 stream.buf = append(stream.buf, c1, c2)
86 func (stream *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
87 stream.buf = append(stream.buf, c1, c2, c3)
90 func (stream *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
91 stream.buf = append(stream.buf, c1, c2, c3, c4)
94 func (stream *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
95 stream.buf = append(stream.buf, c1, c2, c3, c4, c5)
98 // Flush writes any buffered data to the underlying io.Writer.
99 func (stream *Stream) Flush() error {
100 if stream.out == nil {
103 if stream.Error != nil {
106 n, err := stream.out.Write(stream.buf)
108 if stream.Error == nil {
113 stream.buf = stream.buf[n:]
117 // WriteRaw write string out without quotes, just like []byte
118 func (stream *Stream) WriteRaw(s string) {
119 stream.buf = append(stream.buf, s...)
122 // WriteNil write null to stream
123 func (stream *Stream) WriteNil() {
124 stream.writeFourBytes('n', 'u', 'l', 'l')
127 // WriteTrue write true to stream
128 func (stream *Stream) WriteTrue() {
129 stream.writeFourBytes('t', 'r', 'u', 'e')
132 // WriteFalse write false to stream
133 func (stream *Stream) WriteFalse() {
134 stream.writeFiveBytes('f', 'a', 'l', 's', 'e')
137 // WriteBool write true or false into stream
138 func (stream *Stream) WriteBool(val bool) {
146 // WriteObjectStart write { with possible indention
147 func (stream *Stream) WriteObjectStart() {
148 stream.indention += stream.cfg.indentionStep
149 stream.writeByte('{')
150 stream.writeIndention(0)
153 // WriteObjectField write "field": with possible indention
154 func (stream *Stream) WriteObjectField(field string) {
155 stream.WriteString(field)
156 if stream.indention > 0 {
157 stream.writeTwoBytes(':', ' ')
159 stream.writeByte(':')
163 // WriteObjectEnd write } with possible indention
164 func (stream *Stream) WriteObjectEnd() {
165 stream.writeIndention(stream.cfg.indentionStep)
166 stream.indention -= stream.cfg.indentionStep
167 stream.writeByte('}')
170 // WriteEmptyObject write {}
171 func (stream *Stream) WriteEmptyObject() {
172 stream.writeByte('{')
173 stream.writeByte('}')
176 // WriteMore write , with possible indention
177 func (stream *Stream) WriteMore() {
178 stream.writeByte(',')
179 stream.writeIndention(0)
183 // WriteArrayStart write [ with possible indention
184 func (stream *Stream) WriteArrayStart() {
185 stream.indention += stream.cfg.indentionStep
186 stream.writeByte('[')
187 stream.writeIndention(0)
190 // WriteEmptyArray write []
191 func (stream *Stream) WriteEmptyArray() {
192 stream.writeTwoBytes('[', ']')
195 // WriteArrayEnd write ] with possible indention
196 func (stream *Stream) WriteArrayEnd() {
197 stream.writeIndention(stream.cfg.indentionStep)
198 stream.indention -= stream.cfg.indentionStep
199 stream.writeByte(']')
202 func (stream *Stream) writeIndention(delta int) {
203 if stream.indention == 0 {
206 stream.writeByte('\n')
207 toWrite := stream.indention - delta
208 for i := 0; i < toWrite; i++ {
209 stream.buf = append(stream.buf, ' ')