-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
221 lines (161 loc) · 3.66 KB
/
encoder.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 jsoon
import "io"
// NewEncoder will return a new encoder
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{
w: w,
}
}
// Encoder will handle the encoding
type Encoder struct {
w io.Writer
buf *buffer
depth int
child int
}
// Encode will marshal an Encodee
func (e *Encoder) Encode(value Encodee) (err error) {
if e.depth == 0 {
// Acquire buffer for this depth
e.buf = p.Acquire()
}
// Increase depth
e.depth++
// Get parent's child value
pc := e.child
// Set child value to 0, since this is a new object
e.child = 0
e.buf.WriteByte(charOpenCurly)
if err = value.MarshalJsoon(e); err != nil {
return
}
e.buf.WriteByte(charCloseCurly)
if _, err = e.w.Write(e.buf.Bytes()); err != nil {
return
}
e.buf.Reset()
// Set child value to parent's child value
e.child = pc
// Reduce depth to the parent's level
e.depth--
if e.depth == 0 {
p.Release(e.buf)
e.buf = nil
}
return
}
// Object will marshal an Encodee
func (e *Encoder) Object(key string, value Encodee) (err error) {
if e.depth == 0 {
// Acquire buffer for this depth
e.buf = p.Acquire()
}
// Increase depth
e.depth++
// Get parent's child value
pc := e.child
if e.child > 0 {
e.buf.WriteByte(charComma)
}
// Set child value to 0, since this is a new object
e.child = 0
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":{`)
if err = value.MarshalJsoon(e); err != nil {
return
}
e.buf.WriteByte(charCloseCurly)
if _, err = e.w.Write(e.buf.Bytes()); err != nil {
return
}
e.buf.Reset()
// Reduce depth to the parent's level
e.depth--
// Set child value to parent's child value
e.child = pc
e.child++
return
}
// Array will marshal an array
func (e *Encoder) Array(key string, value ArrayEncodee) (err error) {
if e.depth == 0 {
// Acquire buffer for this depth
e.buf = p.Acquire()
}
// Increase depth
e.depth++
// Get parent's child value
pc := e.child
if pc > 0 {
e.buf.WriteByte(charComma)
}
// Set child value to 0, since this is a new object
e.child = 0
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":[`)
ae := p.AcquireAE(e)
if err = value.MarshalJsoon(ae); err != nil {
return
}
p.ReleaseAE(ae)
e.buf.WriteByte(charCloseBracket)
if _, err = e.w.Write(e.buf.Bytes()); err != nil {
return
}
e.buf.Reset()
// Reduce depth to the parent's level
e.depth--
// Set child value to parent's child value
e.child = pc
e.child++
return
}
// String will escape and marshal a string
func (e *Encoder) String(key, value string) {
if e.child > 0 {
e.buf.WriteByte(charComma)
}
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":"`)
e.buf.WriteEscapedString(value)
e.buf.WriteByte(charDoubleQuote)
e.child++
}
// UnsafeString will will marshal a string without escaping
// Note: Only use this if you are CERTAIN that your value does not contain any quotes
func (e *Encoder) UnsafeString(key, value string) {
if e.child > 0 {
e.buf.WriteByte(charComma)
}
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":"`)
e.buf.WriteString(value)
e.buf.WriteByte(charDoubleQuote)
e.child++
}
// Number will marshal a number
func (e *Encoder) Number(key string, value float64) {
if e.child > 0 {
e.buf.WriteByte(charComma)
}
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":`)
e.buf.WriteFloat64(value)
e.child++
}
// Bool will marshal a boolean
func (e *Encoder) Bool(key string, value bool) {
if e.child > 0 {
e.buf.WriteByte(charComma)
}
e.buf.WriteByte(charDoubleQuote)
e.buf.WriteString(key)
e.buf.WriteString(`":`)
e.buf.WriteBool(value)
e.child++
}