forked from go-faster/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.go
212 lines (184 loc) · 4.85 KB
/
enc.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
package jx
import "io"
// Encoder encodes json to underlying buffer.
//
// Zero value is valid.
type Encoder struct {
w Writer // underlying writer
indent int // count of spaces for single indentation level
// first handles state for comma and indentation writing.
//
// New Object or Array appends new level to this slice, and
// last element of this slice denotes whether first element was written.
//
// We write commas only before non-first element of Array or Object.
//
// See comma, begin, end and FieldStart for implementation details.
//
// Note: probably, this can be optimized as bit set to ease memory
// consumption.
//
// See https://yourbasic.org/algorithms/your-basic-int/#simple-sets
first []bool
}
// Write implements io.Writer.
func (e *Encoder) Write(p []byte) (n int, err error) {
return e.w.Write(p)
}
// WriteTo implements io.WriterTo.
func (e *Encoder) WriteTo(w io.Writer) (n int64, err error) {
return e.w.WriteTo(w)
}
// SetIdent sets length of single indentation step.
func (e *Encoder) SetIdent(n int) {
e.indent = n
}
// String returns string of underlying buffer.
func (e Encoder) String() string {
return e.w.String()
}
// Reset resets underlying buffer.
//
// If e is in streaming mode, it is reset to non-streaming mode.
func (e *Encoder) Reset() {
e.w.Reset()
e.first = e.first[:0]
}
// ResetWriter resets underlying buffer and sets output writer.
func (e *Encoder) ResetWriter(out io.Writer) {
e.w.ResetWriter(out)
e.first = e.first[:0]
}
// Grow grows the underlying buffer
func (e *Encoder) Grow(n int) {
e.w.Grow(n)
}
// Bytes returns underlying buffer.
func (e Encoder) Bytes() []byte { return e.w.Buf }
// SetBytes sets underlying buffer.
func (e *Encoder) SetBytes(buf []byte) { e.w.Buf = buf }
// byte writes a single byte.
func (e *Encoder) byte(c byte) bool {
return e.w.byte(c)
}
// RawStr writes string as raw json.
func (e *Encoder) RawStr(v string) bool {
return e.comma() ||
e.w.RawStr(v)
}
// Raw writes byte slice as raw json.
func (e *Encoder) Raw(b []byte) bool {
return e.comma() ||
e.w.Raw(b)
}
// Null writes null.
func (e *Encoder) Null() bool {
return e.comma() ||
e.w.Null()
}
// Bool encodes boolean.
func (e *Encoder) Bool(v bool) bool {
return e.comma() ||
e.w.Bool(v)
}
// ObjStart writes object start, performing indentation if needed.
//
// Use Obj as convenience helper for writing objects.
func (e *Encoder) ObjStart() (fail bool) {
fail = e.comma() || e.w.ObjStart()
e.begin()
return fail || e.writeIndent()
}
// FieldStart encodes field name and writes colon.
//
// For non-zero indentation also writes single space after colon.
//
// Use Field as convenience helper for encoding fields.
func (e *Encoder) FieldStart(field string) (fail bool) {
fail = e.comma() || e.w.FieldStart(field)
if e.indent > 0 {
fail = fail || e.byte(' ')
}
if len(e.first) > 0 {
e.first[e.current()] = true
}
return fail
}
// Field encodes field start and then invokes callback.
//
// Has ~5ns overhead over FieldStart.
func (e *Encoder) Field(name string, f func(e *Encoder)) (fail bool) {
fail = e.FieldStart(name)
// TODO(tdakkota): return bool from f?
f(e)
return fail
}
// ObjEnd writes end of object token, performing indentation if needed.
//
// Use Obj as convenience helper for writing objects.
func (e *Encoder) ObjEnd() bool {
e.end()
return e.writeIndent() || e.w.ObjEnd()
}
// ObjEmpty writes empty object.
func (e *Encoder) ObjEmpty() bool {
return e.comma() ||
e.w.ObjStart() ||
e.w.ObjEnd()
}
// Obj writes start of object, invokes callback and writes end of object.
//
// If callback is nil, writes empty object.
func (e *Encoder) Obj(f func(e *Encoder)) (fail bool) {
if f == nil {
return e.ObjEmpty()
}
fail = e.ObjStart()
// TODO(tdakkota): return bool from f?
f(e)
return fail || e.ObjEnd()
}
// ArrStart writes start of array, performing indentation if needed.
//
// Use Arr as convenience helper for writing arrays.
func (e *Encoder) ArrStart() (fail bool) {
fail = e.comma() || e.w.ArrStart()
e.begin()
return fail || e.writeIndent()
}
// ArrEmpty writes empty array.
func (e *Encoder) ArrEmpty() bool {
return e.comma() ||
e.w.ArrStart() ||
e.w.ArrEnd()
}
// ArrEnd writes end of array, performing indentation if needed.
//
// Use Arr as convenience helper for writing arrays.
func (e *Encoder) ArrEnd() bool {
e.end()
return e.writeIndent() ||
e.w.ArrEnd()
}
// Arr writes start of array, invokes callback and writes end of array.
//
// If callback is nil, writes empty array.
func (e *Encoder) Arr(f func(e *Encoder)) (fail bool) {
if f == nil {
return e.ArrEmpty()
}
fail = e.ArrStart()
// TODO(tdakkota): return bool from f?
f(e)
return fail || e.ArrEnd()
}
func (e *Encoder) writeIndent() (fail bool) {
if e.indent == 0 {
return false
}
fail = e.byte('\n')
for i := 0; i < len(e.first)*e.indent && !fail; i++ {
fail = fail || e.byte(' ')
}
return fail
}