podman
210 строк · 5.2 Кб
1package jsoniter
2
3import (
4"io"
5)
6
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.
9type Stream struct {
10cfg *frozenConfig
11out io.Writer
12buf []byte
13Error error
14indention int
15Attachment interface{} // open for customized encoder
16}
17
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.
22func NewStream(cfg API, out io.Writer, bufSize int) *Stream {
23return &Stream{
24cfg: cfg.(*frozenConfig),
25out: out,
26buf: make([]byte, 0, bufSize),
27Error: nil,
28indention: 0,
29}
30}
31
32// Pool returns a pool can provide more stream with same configuration
33func (stream *Stream) Pool() StreamPool {
34return stream.cfg
35}
36
37// Reset reuse this stream instance by assign a new writer
38func (stream *Stream) Reset(out io.Writer) {
39stream.out = out
40stream.buf = stream.buf[:0]
41}
42
43// Available returns how many bytes are unused in the buffer.
44func (stream *Stream) Available() int {
45return cap(stream.buf) - len(stream.buf)
46}
47
48// Buffered returns the number of bytes that have been written into the current buffer.
49func (stream *Stream) Buffered() int {
50return len(stream.buf)
51}
52
53// Buffer if writer is nil, use this method to take the result
54func (stream *Stream) Buffer() []byte {
55return stream.buf
56}
57
58// SetBuffer allows to append to the internal buffer directly
59func (stream *Stream) SetBuffer(buf []byte) {
60stream.buf = buf
61}
62
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.
67func (stream *Stream) Write(p []byte) (nn int, err error) {
68stream.buf = append(stream.buf, p...)
69if stream.out != nil {
70nn, err = stream.out.Write(stream.buf)
71stream.buf = stream.buf[nn:]
72return
73}
74return len(p), nil
75}
76
77// WriteByte writes a single byte.
78func (stream *Stream) writeByte(c byte) {
79stream.buf = append(stream.buf, c)
80}
81
82func (stream *Stream) writeTwoBytes(c1 byte, c2 byte) {
83stream.buf = append(stream.buf, c1, c2)
84}
85
86func (stream *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
87stream.buf = append(stream.buf, c1, c2, c3)
88}
89
90func (stream *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
91stream.buf = append(stream.buf, c1, c2, c3, c4)
92}
93
94func (stream *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
95stream.buf = append(stream.buf, c1, c2, c3, c4, c5)
96}
97
98// Flush writes any buffered data to the underlying io.Writer.
99func (stream *Stream) Flush() error {
100if stream.out == nil {
101return nil
102}
103if stream.Error != nil {
104return stream.Error
105}
106_, err := stream.out.Write(stream.buf)
107if err != nil {
108if stream.Error == nil {
109stream.Error = err
110}
111return err
112}
113stream.buf = stream.buf[:0]
114return nil
115}
116
117// WriteRaw write string out without quotes, just like []byte
118func (stream *Stream) WriteRaw(s string) {
119stream.buf = append(stream.buf, s...)
120}
121
122// WriteNil write null to stream
123func (stream *Stream) WriteNil() {
124stream.writeFourBytes('n', 'u', 'l', 'l')
125}
126
127// WriteTrue write true to stream
128func (stream *Stream) WriteTrue() {
129stream.writeFourBytes('t', 'r', 'u', 'e')
130}
131
132// WriteFalse write false to stream
133func (stream *Stream) WriteFalse() {
134stream.writeFiveBytes('f', 'a', 'l', 's', 'e')
135}
136
137// WriteBool write true or false into stream
138func (stream *Stream) WriteBool(val bool) {
139if val {
140stream.WriteTrue()
141} else {
142stream.WriteFalse()
143}
144}
145
146// WriteObjectStart write { with possible indention
147func (stream *Stream) WriteObjectStart() {
148stream.indention += stream.cfg.indentionStep
149stream.writeByte('{')
150stream.writeIndention(0)
151}
152
153// WriteObjectField write "field": with possible indention
154func (stream *Stream) WriteObjectField(field string) {
155stream.WriteString(field)
156if stream.indention > 0 {
157stream.writeTwoBytes(':', ' ')
158} else {
159stream.writeByte(':')
160}
161}
162
163// WriteObjectEnd write } with possible indention
164func (stream *Stream) WriteObjectEnd() {
165stream.writeIndention(stream.cfg.indentionStep)
166stream.indention -= stream.cfg.indentionStep
167stream.writeByte('}')
168}
169
170// WriteEmptyObject write {}
171func (stream *Stream) WriteEmptyObject() {
172stream.writeByte('{')
173stream.writeByte('}')
174}
175
176// WriteMore write , with possible indention
177func (stream *Stream) WriteMore() {
178stream.writeByte(',')
179stream.writeIndention(0)
180}
181
182// WriteArrayStart write [ with possible indention
183func (stream *Stream) WriteArrayStart() {
184stream.indention += stream.cfg.indentionStep
185stream.writeByte('[')
186stream.writeIndention(0)
187}
188
189// WriteEmptyArray write []
190func (stream *Stream) WriteEmptyArray() {
191stream.writeTwoBytes('[', ']')
192}
193
194// WriteArrayEnd write ] with possible indention
195func (stream *Stream) WriteArrayEnd() {
196stream.writeIndention(stream.cfg.indentionStep)
197stream.indention -= stream.cfg.indentionStep
198stream.writeByte(']')
199}
200
201func (stream *Stream) writeIndention(delta int) {
202if stream.indention == 0 {
203return
204}
205stream.writeByte('\n')
206toWrite := stream.indention - delta
207for i := 0; i < toWrite; i++ {
208stream.buf = append(stream.buf, ' ')
209}
210}
211