1 package json
2
3 import (
4 "fmt"
5 "math"
6 "net"
7 "strconv"
8 )
9
10
11 func (Encoder) AppendNil(dst []byte) []byte {
12 return append(dst, "null"...)
13 }
14
15
16 func (Encoder) AppendBeginMarker(dst []byte) []byte {
17 return append(dst, '{')
18 }
19
20
21 func (Encoder) AppendEndMarker(dst []byte) []byte {
22 return append(dst, '}')
23 }
24
25
26 func (Encoder) AppendLineBreak(dst []byte) []byte {
27 return append(dst, '\n')
28 }
29
30
31 func (Encoder) AppendArrayStart(dst []byte) []byte {
32 return append(dst, '[')
33 }
34
35
36 func (Encoder) AppendArrayEnd(dst []byte) []byte {
37 return append(dst, ']')
38 }
39
40
41 func (Encoder) AppendArrayDelim(dst []byte) []byte {
42 if len(dst) > 0 {
43 return append(dst, ',')
44 }
45 return dst
46 }
47
48
49
50 func (Encoder) AppendBool(dst []byte, val bool) []byte {
51 return strconv.AppendBool(dst, val)
52 }
53
54
55
56 func (Encoder) AppendBools(dst []byte, vals []bool) []byte {
57 if len(vals) == 0 {
58 return append(dst, '[', ']')
59 }
60 dst = append(dst, '[')
61 dst = strconv.AppendBool(dst, vals[0])
62 if len(vals) > 1 {
63 for _, val := range vals[1:] {
64 dst = strconv.AppendBool(append(dst, ','), val)
65 }
66 }
67 dst = append(dst, ']')
68 return dst
69 }
70
71
72
73 func (Encoder) AppendInt(dst []byte, val int) []byte {
74 return strconv.AppendInt(dst, int64(val), 10)
75 }
76
77
78
79 func (Encoder) AppendInts(dst []byte, vals []int) []byte {
80 if len(vals) == 0 {
81 return append(dst, '[', ']')
82 }
83 dst = append(dst, '[')
84 dst = strconv.AppendInt(dst, int64(vals[0]), 10)
85 if len(vals) > 1 {
86 for _, val := range vals[1:] {
87 dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
88 }
89 }
90 dst = append(dst, ']')
91 return dst
92 }
93
94
95
96 func (Encoder) AppendInt8(dst []byte, val int8) []byte {
97 return strconv.AppendInt(dst, int64(val), 10)
98 }
99
100
101
102 func (Encoder) AppendInts8(dst []byte, vals []int8) []byte {
103 if len(vals) == 0 {
104 return append(dst, '[', ']')
105 }
106 dst = append(dst, '[')
107 dst = strconv.AppendInt(dst, int64(vals[0]), 10)
108 if len(vals) > 1 {
109 for _, val := range vals[1:] {
110 dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
111 }
112 }
113 dst = append(dst, ']')
114 return dst
115 }
116
117
118
119 func (Encoder) AppendInt16(dst []byte, val int16) []byte {
120 return strconv.AppendInt(dst, int64(val), 10)
121 }
122
123
124
125 func (Encoder) AppendInts16(dst []byte, vals []int16) []byte {
126 if len(vals) == 0 {
127 return append(dst, '[', ']')
128 }
129 dst = append(dst, '[')
130 dst = strconv.AppendInt(dst, int64(vals[0]), 10)
131 if len(vals) > 1 {
132 for _, val := range vals[1:] {
133 dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
134 }
135 }
136 dst = append(dst, ']')
137 return dst
138 }
139
140
141
142 func (Encoder) AppendInt32(dst []byte, val int32) []byte {
143 return strconv.AppendInt(dst, int64(val), 10)
144 }
145
146
147
148 func (Encoder) AppendInts32(dst []byte, vals []int32) []byte {
149 if len(vals) == 0 {
150 return append(dst, '[', ']')
151 }
152 dst = append(dst, '[')
153 dst = strconv.AppendInt(dst, int64(vals[0]), 10)
154 if len(vals) > 1 {
155 for _, val := range vals[1:] {
156 dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
157 }
158 }
159 dst = append(dst, ']')
160 return dst
161 }
162
163
164
165 func (Encoder) AppendInt64(dst []byte, val int64) []byte {
166 return strconv.AppendInt(dst, val, 10)
167 }
168
169
170
171 func (Encoder) AppendInts64(dst []byte, vals []int64) []byte {
172 if len(vals) == 0 {
173 return append(dst, '[', ']')
174 }
175 dst = append(dst, '[')
176 dst = strconv.AppendInt(dst, vals[0], 10)
177 if len(vals) > 1 {
178 for _, val := range vals[1:] {
179 dst = strconv.AppendInt(append(dst, ','), val, 10)
180 }
181 }
182 dst = append(dst, ']')
183 return dst
184 }
185
186
187
188 func (Encoder) AppendUint(dst []byte, val uint) []byte {
189 return strconv.AppendUint(dst, uint64(val), 10)
190 }
191
192
193
194 func (Encoder) AppendUints(dst []byte, vals []uint) []byte {
195 if len(vals) == 0 {
196 return append(dst, '[', ']')
197 }
198 dst = append(dst, '[')
199 dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
200 if len(vals) > 1 {
201 for _, val := range vals[1:] {
202 dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
203 }
204 }
205 dst = append(dst, ']')
206 return dst
207 }
208
209
210
211 func (Encoder) AppendUint8(dst []byte, val uint8) []byte {
212 return strconv.AppendUint(dst, uint64(val), 10)
213 }
214
215
216
217 func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
218 if len(vals) == 0 {
219 return append(dst, '[', ']')
220 }
221 dst = append(dst, '[')
222 dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
223 if len(vals) > 1 {
224 for _, val := range vals[1:] {
225 dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
226 }
227 }
228 dst = append(dst, ']')
229 return dst
230 }
231
232
233
234 func (Encoder) AppendUint16(dst []byte, val uint16) []byte {
235 return strconv.AppendUint(dst, uint64(val), 10)
236 }
237
238
239
240 func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
241 if len(vals) == 0 {
242 return append(dst, '[', ']')
243 }
244 dst = append(dst, '[')
245 dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
246 if len(vals) > 1 {
247 for _, val := range vals[1:] {
248 dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
249 }
250 }
251 dst = append(dst, ']')
252 return dst
253 }
254
255
256
257 func (Encoder) AppendUint32(dst []byte, val uint32) []byte {
258 return strconv.AppendUint(dst, uint64(val), 10)
259 }
260
261
262
263 func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
264 if len(vals) == 0 {
265 return append(dst, '[', ']')
266 }
267 dst = append(dst, '[')
268 dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
269 if len(vals) > 1 {
270 for _, val := range vals[1:] {
271 dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
272 }
273 }
274 dst = append(dst, ']')
275 return dst
276 }
277
278
279
280 func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
281 return strconv.AppendUint(dst, val, 10)
282 }
283
284
285
286 func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
287 if len(vals) == 0 {
288 return append(dst, '[', ']')
289 }
290 dst = append(dst, '[')
291 dst = strconv.AppendUint(dst, vals[0], 10)
292 if len(vals) > 1 {
293 for _, val := range vals[1:] {
294 dst = strconv.AppendUint(append(dst, ','), val, 10)
295 }
296 }
297 dst = append(dst, ']')
298 return dst
299 }
300
301 func appendFloat(dst []byte, val float64, bitSize int) []byte {
302
303
304
305 switch {
306 case math.IsNaN(val):
307 return append(dst, `"NaN"`...)
308 case math.IsInf(val, 1):
309 return append(dst, `"+Inf"`...)
310 case math.IsInf(val, -1):
311 return append(dst, `"-Inf"`...)
312 }
313 return strconv.AppendFloat(dst, val, 'f', -1, bitSize)
314 }
315
316
317
318 func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
319 return appendFloat(dst, float64(val), 32)
320 }
321
322
323
324 func (Encoder) AppendFloats32(dst []byte, vals []float32) []byte {
325 if len(vals) == 0 {
326 return append(dst, '[', ']')
327 }
328 dst = append(dst, '[')
329 dst = appendFloat(dst, float64(vals[0]), 32)
330 if len(vals) > 1 {
331 for _, val := range vals[1:] {
332 dst = appendFloat(append(dst, ','), float64(val), 32)
333 }
334 }
335 dst = append(dst, ']')
336 return dst
337 }
338
339
340
341 func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
342 return appendFloat(dst, val, 64)
343 }
344
345
346
347 func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
348 if len(vals) == 0 {
349 return append(dst, '[', ']')
350 }
351 dst = append(dst, '[')
352 dst = appendFloat(dst, vals[0], 64)
353 if len(vals) > 1 {
354 for _, val := range vals[1:] {
355 dst = appendFloat(append(dst, ','), val, 64)
356 }
357 }
358 dst = append(dst, ']')
359 return dst
360 }
361
362
363
364 func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
365 marshaled, err := JSONMarshalFunc(i)
366 if err != nil {
367 return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
368 }
369 return append(dst, marshaled...)
370 }
371
372
373
374 func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
375
376
377
378
379
380 if o[0] == '{' {
381 if len(dst) > 1 {
382 dst = append(dst, ',')
383 }
384 o = o[1:]
385 } else if len(dst) > 1 {
386 dst = append(dst, ',')
387 }
388 return append(dst, o...)
389 }
390
391
392 func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
393 return e.AppendString(dst, ip.String())
394 }
395
396
397 func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
398 return e.AppendString(dst, pfx.String())
399
400 }
401
402
403 func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
404 return e.AppendString(dst, ha.String())
405 }
406
View as plain text