-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.go
625 lines (553 loc) · 16.2 KB
/
json.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Copyright 2020 by David A. Golden. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package jibby
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf16"
)
var errNullEscape = errors.New("string escape mapped to null byte")
// convertValue starts before any bytes of a value have been read. It detects
// the value's type and dispatches to a handler. It supports all JSON value
// types. (The `convertObject` handler handles extended JSON.)
func (d *Decoder) convertValue(out []byte, typeBytePos int) ([]byte, error) {
ch, err := d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
switch ch {
case '{':
// Defer writing type byte to handle extended JSON, which would have a
// non-document type determined later from key inspection.
out, err = d.convertObject(out, typeBytePos)
if err != nil {
return nil, err
}
case '[':
overwriteTypeByte(out, typeBytePos, bsonArray)
out, err = d.convertArray(out)
if err != nil {
return nil, err
}
case 't':
overwriteTypeByte(out, typeBytePos, bsonBoolean)
out, err = d.convertTrue(out)
if err != nil {
return nil, err
}
case 'f':
overwriteTypeByte(out, typeBytePos, bsonBoolean)
out, err = d.convertFalse(out)
if err != nil {
return nil, err
}
case 'n':
overwriteTypeByte(out, typeBytePos, bsonNull)
out, err = d.convertNull(out)
if err != nil {
return nil, err
}
case '"':
overwriteTypeByte(out, typeBytePos, bsonString)
out, err = d.convertString(out)
if err != nil {
return nil, err
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// This starts a number, so we unread the byte and let that handler give
// us any error. We can't write the
// type byte until the number type is determined (int64, int32, double),
// so we pass in the type byte position.
_ = d.json.UnreadByte()
out, err = d.convertNumber(out, typeBytePos)
if err != nil {
return nil, err
}
default:
return nil, d.parseError([]byte{ch}, "invalid character")
}
return out, nil
}
// convertObject starts after the opening brace of an object.
func (d *Decoder) convertObject(out []byte, outerTypeBytePos int) ([]byte, error) {
var ch byte
var err error
// Depth check
d.curDepth++
if d.curDepth > d.maxDepth {
return nil, errors.New("maximum depth exceeded")
}
defer func() { d.curDepth-- }()
// Note position of placeholder for length that we write, but don't
// write yet in case this turns out to be extended JSON.
lengthPos := len(out)
// Check for empty object or start of key
ch, err = d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
switch ch {
case '}':
// Empty object
overwriteTypeByte(out, outerTypeBytePos, bsonDocument)
out = append(out, emptyDoc...)
return out, nil
case '"':
// Put back quote for subsequent object parsing
_ = d.json.UnreadByte()
// If ExtJSON enabled and `handleExtJSON` returns a buffer, then this
// value was extended JSON and the value has been consumed.
if d.extJSONAllowed && outerTypeBytePos != topContainer {
_ = d.json.UnreadByte()
buf, err := d.handleExtJSON(out, outerTypeBytePos)
if err != nil {
return nil, err
}
if buf != nil {
return buf, nil
}
}
// Not extended JSON, so now write the length placeholder
out = append(out, emptyLength...)
overwriteTypeByte(out, outerTypeBytePos, bsonDocument)
default:
return nil, d.parseError([]byte{ch}, "expecting key or end of object")
}
// Convert the first element
out, err = d.convertObjectElement(out)
if err != nil {
return nil, err
}
// Loop, looking for separators or object terminator
LOOP:
for {
ch, err = d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
switch ch {
case ',':
// Convert next element
out, err = d.convertObjectElement(out)
if err != nil {
return nil, err
}
case '}':
break LOOP
default:
return nil, d.parseError([]byte{ch}, "expecting value-separator or end of object")
}
}
// Write null terminator and calculate/update length
out = append(out, nullByte)
objectLength := len(out) - lengthPos
overwriteLength(out, lengthPos, objectLength)
return out, nil
}
func (d *Decoder) convertObjectElement(out []byte) ([]byte, error) {
// Next non-WS character must be quote to start key
ch, err := d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
if ch != '"' {
return nil, d.parseError([]byte{ch}, "expecting opening quote of key")
}
// Record position for the placeholder type byte that we write
typeBytePos := len(out)
out = append(out, emptyType)
// Convert key as Cstring
out, err = d.convertCString(out)
if err != nil {
if err == errNullEscape {
return nil, d.parseError(nil, errNullEscape.Error())
}
return nil, err
}
// Next non-WS char must be ':' for separator
err = d.readNameSeparator()
if err != nil {
return nil, err
}
// Convert next value
out, err = d.convertValue(out, typeBytePos)
if err != nil {
return nil, err
}
return out, nil
}
// convertArray starts after the opening bracket of an array.
func (d *Decoder) convertArray(out []byte) ([]byte, error) {
var ch byte
var err error
// Depth check
d.curDepth++
if d.curDepth > d.maxDepth {
return nil, errors.New("maximum depth exceeded")
}
defer func() { d.curDepth-- }()
// Note position of placeholder for length that we write
lengthPos := len(out)
out = append(out, emptyLength...)
ch, err = d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
// Case: empty array
if ch == ']' {
out = append(out, nullByte)
arrayLength := len(out) - lengthPos
overwriteLength(out, lengthPos, arrayLength)
return out, nil
}
// Not empty: unread the byte for convertValue to check
_ = d.json.UnreadByte()
// Convert the first value
index := 0
out, err = d.convertArrayElement(out, index)
if err != nil {
return nil, err
}
// Loop, looking for separators or array terminator
LOOP:
for {
ch, err = d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
switch ch {
case ',':
// Convert the next value
index++
out, err = d.convertArrayElement(out, index)
if err != nil {
return nil, err
}
case ']':
break LOOP
default:
return nil, d.parseError([]byte{ch}, "expecting value-separator or end of array")
}
}
// Write null terminator and calculate/update length
out = append(out, nullByte)
arrayLength := len(out) - lengthPos
overwriteLength(out, lengthPos, arrayLength)
return out, nil
}
func (d *Decoder) convertArrayElement(out []byte, index int) ([]byte, error) {
// Record position for the placeholder type byte
typeBytePos := len(out)
out = append(out, emptyType)
// Append next key
if index < len(arrayKey) {
out = append(out, arrayKey[index]...)
} else {
out = append(out, []byte(strconv.Itoa(index))...)
}
out = append(out, nullByte)
// Convert next value
out, err := d.convertValue(out, typeBytePos)
if err != nil {
return nil, err
}
return out, nil
}
// convertTrue starts after the 't' for true has been read.
func (d *Decoder) convertTrue(out []byte) ([]byte, error) {
rest, err := d.json.Peek(3)
if err != nil {
return nil, newReadError(err)
}
if len(rest) < 3 {
return nil, newReadError(io.ErrUnexpectedEOF)
}
// already saw 't', looking for "rue"
if rest[0] != 'r' || rest[1] != 'u' || rest[2] != 'e' {
return nil, d.parseError([]byte{'t'}, "expecting true")
}
_, _ = d.json.Discard(3)
out = append(out, 1)
return out, nil
}
// convertFalse starts after the 'f' for false has been read.
func (d *Decoder) convertFalse(out []byte) ([]byte, error) {
rest, err := d.json.Peek(4)
if err != nil {
return nil, newReadError(err)
}
if len(rest) < 4 {
return nil, newReadError(io.ErrUnexpectedEOF)
}
// Already saw 'f', looking for "alse"
if rest[0] != 'a' || rest[1] != 'l' || rest[2] != 's' || rest[3] != 'e' {
return nil, d.parseError([]byte{'f'}, "expecting false")
}
_, _ = d.json.Discard(4)
out = append(out, 0)
return out, nil
}
// convertNull starts after the 'n' for null has been read.
func (d *Decoder) convertNull(out []byte) ([]byte, error) {
rest, err := d.json.Peek(3)
if err != nil {
return nil, newReadError(err)
}
if len(rest) < 3 {
return nil, newReadError(io.ErrUnexpectedEOF)
}
// Already saw 'n', looking for "ull"
if rest[0] != 'u' || rest[1] != 'l' || rest[2] != 'l' {
return nil, d.parseError([]byte{'n'}, "expecting null")
}
_, _ = d.json.Discard(3)
// Nothing to write
return out, nil
}
// convertNumber starts before any of the number has been read. Have to peek
// ahead in the buffer to find the end point and whether to convert as integer
// or floating point. It consumes the number from input when finished.
func (d *Decoder) convertNumber(out []byte, typeBytePos int) ([]byte, error) {
// Peek ahead to find the bytes that make up the number representation.
buf, isFloat, err := d.peekNumber()
if err != nil {
return nil, err
}
if isFloat {
out, err = d.convertFloat(out, typeBytePos, buf)
if err != nil {
return nil, err
}
} else {
// Still don't know if the type is int32 or int64, so delegate.
out, err = d.convertInt(out, typeBytePos, buf)
if err != nil {
return nil, err
}
}
_, _ = d.json.Discard(len(buf))
return out, nil
}
// convertFloat converts the floating-point number in the buffer. It does not
// consume any of the input.
func (d *Decoder) convertFloat(out []byte, typeBytePos int, buf []byte) ([]byte, error) {
n, err := strconv.ParseFloat(string(buf), 64)
if err != nil {
return nil, d.parseError(nil, fmt.Sprintf("float conversion: %v", err))
}
overwriteTypeByte(out, typeBytePos, bsonDouble)
var x [8]byte
xs := x[0:8]
binary.LittleEndian.PutUint64(xs, math.Float64bits(n))
out = append(out, xs...)
return out, nil
}
// convertInt converts the integer number in the buffer. It does not consume
// any of the input.
func (d *Decoder) convertInt(out []byte, typeBytePos int, buf []byte) ([]byte, error) {
n, err := strconv.ParseInt(string(buf), 10, 64)
if err != nil {
if strings.Contains(err.Error(), strconv.ErrRange.Error()) {
// Doesn't fit in int64, so treat as float
return d.convertFloat(out, typeBytePos, buf)
}
return nil, d.parseError(nil, fmt.Sprintf("int conversion: %v", err))
}
if n < math.MinInt32 || n > math.MaxInt32 {
overwriteTypeByte(out, typeBytePos, bsonInt64)
var x [8]byte
xs := x[0:8]
binary.LittleEndian.PutUint64(xs, uint64(n))
out = append(out, xs...)
return out, nil
}
var x [4]byte
overwriteTypeByte(out, typeBytePos, bsonInt32)
xs := x[0:4]
binary.LittleEndian.PutUint32(xs, uint32(n))
out = append(out, xs...)
return out, nil
}
// convertCString starts after the opening quote of a string. It peeks ahead in
// 64 byte chunks, consuming input when writing to the output. It handles JSON
// string escape sequences, which may require consuming a partial chunk and
// peeking ahead to see the rest of an escape sequence. When finished, the
// string and its closing quote have been consumed from the input.
func (d *Decoder) convertCString(out []byte) ([]byte, error) {
var terminated bool
var sawNullEscape bool
// charsNeeded indicates how much we expect to peek ahead. Normally, we
// always expect to peek at least 1 ahead (for the closing quote), but when
// handling escape sequences, that may change. Being unable to peek the
// desired amount ahead indicates unexpected EOF.
var charsNeeded = 1
for !terminated {
// peek ahead 64 bytes
buf, err := d.json.Peek(64)
if err != nil {
// here, io.EOF is OK, since we're only peeking and may hit end of
// object
if err != io.EOF {
return nil, err
}
}
// if not enough chars, input ended before closing quote or end of
// escape sequence
if len(buf) < charsNeeded {
return nil, newReadError(io.ErrUnexpectedEOF)
}
var i int
INNER:
for i = 0; i < len(buf); i++ {
switch buf[i] {
case '\\':
// need at least two chars in buf
if len(buf)-i < 2 {
// not enough characters left, so break out to repeat peek
// from the backslash but requiring at least two characters
charsNeeded = 2
break INNER
}
switch buf[i+1] {
case '"', '\\', '/':
out = append(out, buf[i+1])
i++
case 'b':
out = append(out, '\b')
i++
case 'f':
out = append(out, '\f')
i++
case 'n':
out = append(out, '\n')
i++
case 'r':
out = append(out, '\r')
i++
case 't':
out = append(out, '\t')
i++
case 'u':
// "\uXXXX" needs 6 chars total from i, otherwise break out
// and repeek, needing 6 chars
if len(buf)-i < 6 {
charsNeeded = 6
break INNER
}
// convert next 4 bytes to rune and append it as UTF-8
n, err := strconv.ParseUint(string(buf[i+2:i+6]), 16, 32)
if err != nil {
_, _ = d.json.Discard(i)
return nil, d.parseError(nil, fmt.Sprintf("converting unicode escape: %v", err))
}
r := rune(int32(n))
// a surrogate pair requires another \uXXXX
if utf16.IsSurrogate(r) {
// "\uXXXX\uYYYY" needs up to 12 chars total. If we
// don't have it, we want to re-peek to get another
// batch of up to 64 bytes. We'll start by requiring 7,
// the first surrogate plus a either a trailing quote or
// a new escape, which is the shortest possible legal
// string from this position.
if len(buf)-i < 7 {
charsNeeded = 7
break INNER
}
// Store first rune; prepare to fallback to replacement
// char if any part of second rune handling fails.
r1 := r
r = unicode.ReplacementChar
if buf[i+6] == '\\' {
// If we're here, we know we need at least one more
// to determine the kind of escape it is.
if len(buf)-i < 8 {
charsNeeded = 8
break INNER
}
if buf[i+7] == 'u' {
// If we're here, then we know we need the full
// 12 bytes for `\uXXXX\uXXXX`.
if len(buf)-i < 12 {
charsNeeded = 12
break INNER
}
n, err := strconv.ParseUint(string(buf[i+8:i+12]), 16, 32)
if err != nil {
_, _ = d.json.Discard(i + 6)
return nil, d.parseError(nil, fmt.Sprintf("converting unicode escape: %v", err))
}
r2 := rune(int32(n))
// If r2 is a surrogate, we know we can advance past
// the first surrogate and decode them together. If
// not, then we don't advance: we'll emit a
// replacement character and skip only the broken
// first pair. This new escape will be dealt with
// on the next pass through the loop.
if utf16.IsSurrogate(r2) {
i += 6
r = utf16.DecodeRune(r1, r2)
}
}
}
}
if r == 0 {
sawNullEscape = true
}
i += 5
out = append(out, []byte(string(r))...)
default:
msg := fmt.Sprintf("unknown escape '%s'", string(buf[i+1]))
_, _ = d.json.Discard(i)
return nil, d.parseError(nil, msg)
}
// Escape is done: go back to needing only one char at a time.
charsNeeded = 1
case '"':
terminated = true
break INNER
default:
if buf[i] < ' ' {
_, _ = d.json.Discard(i)
return nil, d.parseError(nil, "control characters not allowed in strings")
}
out = append(out, buf[i])
}
}
// If terminated, closing quote is at index i, so discard i + 1 bytes to include it,
// otherwise only discard i bytes to skip the text we've copied.
if terminated {
_, _ = d.json.Discard(i + 1)
} else {
_, _ = d.json.Discard(i)
}
}
// C-string null terminator
out = append(out, 0)
if sawNullEscape {
return out, errNullEscape
}
return out, nil
}
// convertString starts after the opening quote of a string. It works like
// convertCString except it prepends the length of the string.
func (d *Decoder) convertString(out []byte) ([]byte, error) {
lengthPos := len(out)
out = append(out, emptyLength...)
out, err := d.convertCString(out)
// For String, a null byte in the string is allowed
if err != nil && err != errNullEscape {
return nil, err
}
strLength := len(out) - lengthPos - 4
overwriteLength(out, lengthPos, strLength)
return out, nil
}