-
Notifications
You must be signed in to change notification settings - Fork 2
/
streamer.go
221 lines (194 loc) · 3.89 KB
/
streamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package jzon
import (
"io"
)
// Streamer is a chained class for encoding object to json
type Streamer struct {
cfg *EncoderConfig
writer io.Writer
buffer []byte
Error error
poped bool
// TODO: 1. type of context?
// TODO: 2. should context be reset as well?
Context interface{} // custom stream context
// runtime
escapeHTML bool
safeSet []string
// prefix string
// indent string
}
// NewStreamer returns a new streamer
func NewStreamer() *Streamer {
return DefaultEncoderConfig.NewStreamer()
}
// Release the streamer, the streamer should not be used after call
func (s *Streamer) Release() {
s.cfg.returnStreamer(s)
}
func (s *Streamer) reset() {
s.writer = nil
s.Error = nil
s.poped = false
s.buffer = s.buffer[:0]
s.Context = nil
}
// Reset resets the streamer with a new writer
func (s *Streamer) Reset(w io.Writer) {
s.reset()
s.writer = w
}
// EscapeHTML set if the string should be html-escaped
func (s *Streamer) EscapeHTML(on bool) {
s.escapeHTML = on
if on {
s.safeSet = htmlSafeSet[:]
} else {
s.safeSet = safeSet[:]
}
}
// func (s *Streamer) SetIndent(prefix, indent string) {
// s.prefix = prefix
// s.indent = indent
// }
// Flush flushes from buffer to the writer
func (s *Streamer) Flush() error {
if s.Error != nil {
return s.Error
}
if s.writer == nil {
return ErrNoAttachedWriter
}
l := len(s.buffer)
// see comment of io.Writer
n, err := s.writer.Write(s.buffer)
if n < l {
copy(s.buffer, s.buffer[n:])
s.buffer = s.buffer[:l-n]
} else {
s.buffer = s.buffer[:0]
}
return err
}
func (s *Streamer) onVal() {
if s.poped {
s.buffer = append(s.buffer, ',')
} else {
s.poped = true
}
}
// RawString writes a raw object (in string)
func (s *Streamer) RawString(raw string) *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, raw...)
return s
}
// Raw writes a raw object (in byte slice)
func (s *Streamer) Raw(raw []byte) *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, raw...)
return s
}
func (s *Streamer) null() {
s.onVal()
s.buffer = append(s.buffer, 'n', 'u', 'l', 'l')
}
// Null writes a `null`
func (s *Streamer) Null() *Streamer {
if s.Error != nil {
return s
}
s.null()
return s
}
// True writes a `true`
func (s *Streamer) True() *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, 't', 'r', 'u', 'e')
return s
}
// False writes a `false`
func (s *Streamer) False() *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, 'f', 'a', 'l', 's', 'e')
return s
}
// Bool writes a boolean value
func (s *Streamer) Bool(b bool) *Streamer {
if b {
return s.True()
}
return s.False()
}
// ObjectStart starts to write an object
func (s *Streamer) ObjectStart() *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, '{')
s.poped = false
return s
}
// Field writes an object field
func (s *Streamer) Field(field string) *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = encodeString(s.buffer, field, s.safeSet)
s.buffer = append(s.buffer, ':')
s.poped = false
return s
}
// RawField writes an object field (in raw byte slice)
func (s *Streamer) RawField(b []byte) *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, b...)
s.buffer = append(s.buffer, ':')
s.poped = false
return s
}
// ObjectEnd ends the object writing
func (s *Streamer) ObjectEnd() *Streamer {
if s.Error != nil {
return s
}
s.buffer = append(s.buffer, '}')
s.poped = true
return s
}
// ArrayStart starts to write an array
func (s *Streamer) ArrayStart() *Streamer {
if s.Error != nil {
return s
}
s.onVal()
s.buffer = append(s.buffer, '[')
s.poped = false
return s
}
// ArrayEnd ends the array writing
func (s *Streamer) ArrayEnd() *Streamer {
if s.Error != nil {
return s
}
s.buffer = append(s.buffer, ']')
s.poped = true
return s
}