1
2
3
4
5 package json
6
7
8
9
10
11
12
13
14
15
16 import (
17 "strconv"
18 "sync"
19 )
20
21
22 func Valid(data []byte) bool {
23 scan := newScanner()
24 defer freeScanner(scan)
25 return checkValid(data, scan) == nil
26 }
27
28
29
30 func checkValid(data []byte, scan *scanner) error {
31 scan.reset()
32 for _, c := range data {
33 scan.bytes++
34 if scan.step(scan, c) == scanError {
35 return scan.err
36 }
37 }
38 if scan.eof() == scanError {
39 return scan.err
40 }
41 return nil
42 }
43
44
45 type SyntaxError struct {
46 msg string
47 Offset int64
48 }
49
50 func (e *SyntaxError) Error() string { return e.msg }
51
52
53
54
55
56
57
58
59
60
61
62
63
64 type scanner struct {
65
66
67
68
69 step func(*scanner, byte) int
70
71
72 endTop bool
73
74
75 parseState []int
76
77
78 err error
79
80
81
82 bytes int64
83 }
84
85 var scannerPool = sync.Pool{
86 New: func() interface{} {
87 return &scanner{}
88 },
89 }
90
91 func newScanner() *scanner {
92 scan := scannerPool.Get().(*scanner)
93
94 scan.bytes = 0
95 scan.reset()
96 return scan
97 }
98
99 func freeScanner(scan *scanner) {
100
101 if len(scan.parseState) > 1024 {
102 scan.parseState = nil
103 }
104 scannerPool.Put(scan)
105 }
106
107
108
109
110
111
112
113
114 const (
115
116 scanContinue = iota
117 scanBeginLiteral
118 scanBeginObject
119 scanObjectKey
120 scanObjectValue
121 scanEndObject
122 scanBeginArray
123 scanArrayValue
124 scanEndArray
125 scanSkipSpace
126
127
128 scanEnd
129 scanError
130 )
131
132
133
134
135
136 const (
137 parseObjectKey = iota
138 parseObjectValue
139 parseArrayValue
140 )
141
142
143
144 const maxNestingDepth = 10000
145
146
147
148 func (s *scanner) reset() {
149 s.step = stateBeginValue
150 s.parseState = s.parseState[0:0]
151 s.err = nil
152 s.endTop = false
153 }
154
155
156
157 func (s *scanner) eof() int {
158 if s.err != nil {
159 return scanError
160 }
161 if s.endTop {
162 return scanEnd
163 }
164 s.step(s, ' ')
165 if s.endTop {
166 return scanEnd
167 }
168 if s.err == nil {
169 s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
170 }
171 return scanError
172 }
173
174
175
176 func (s *scanner) pushParseState(c byte, newParseState int, successState int) int {
177 s.parseState = append(s.parseState, newParseState)
178 if len(s.parseState) <= maxNestingDepth {
179 return successState
180 }
181 return s.error(c, "exceeded max depth")
182 }
183
184
185
186 func (s *scanner) popParseState() {
187 n := len(s.parseState) - 1
188 s.parseState = s.parseState[0:n]
189 if n == 0 {
190 s.step = stateEndTop
191 s.endTop = true
192 } else {
193 s.step = stateEndValue
194 }
195 }
196
197 func isSpace(c byte) bool {
198 return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n')
199 }
200
201
202 func stateBeginValueOrEmpty(s *scanner, c byte) int {
203 if isSpace(c) {
204 return scanSkipSpace
205 }
206 if c == ']' {
207 return stateEndValue(s, c)
208 }
209 return stateBeginValue(s, c)
210 }
211
212
213 func stateBeginValue(s *scanner, c byte) int {
214 if isSpace(c) {
215 return scanSkipSpace
216 }
217 switch c {
218 case '{':
219 s.step = stateBeginStringOrEmpty
220 return s.pushParseState(c, parseObjectKey, scanBeginObject)
221 case '[':
222 s.step = stateBeginValueOrEmpty
223 return s.pushParseState(c, parseArrayValue, scanBeginArray)
224 case '"':
225 s.step = stateInString
226 return scanBeginLiteral
227 case '-':
228 s.step = stateNeg
229 return scanBeginLiteral
230 case '0':
231 s.step = state0
232 return scanBeginLiteral
233 case 't':
234 s.step = stateT
235 return scanBeginLiteral
236 case 'f':
237 s.step = stateF
238 return scanBeginLiteral
239 case 'n':
240 s.step = stateN
241 return scanBeginLiteral
242 }
243 if '1' <= c && c <= '9' {
244 s.step = state1
245 return scanBeginLiteral
246 }
247 return s.error(c, "looking for beginning of value")
248 }
249
250
251 func stateBeginStringOrEmpty(s *scanner, c byte) int {
252 if isSpace(c) {
253 return scanSkipSpace
254 }
255 if c == '}' {
256 n := len(s.parseState)
257 s.parseState[n-1] = parseObjectValue
258 return stateEndValue(s, c)
259 }
260 return stateBeginString(s, c)
261 }
262
263
264 func stateBeginString(s *scanner, c byte) int {
265 if isSpace(c) {
266 return scanSkipSpace
267 }
268 if c == '"' {
269 s.step = stateInString
270 return scanBeginLiteral
271 }
272 return s.error(c, "looking for beginning of object key string")
273 }
274
275
276
277 func stateEndValue(s *scanner, c byte) int {
278 n := len(s.parseState)
279 if n == 0 {
280
281 s.step = stateEndTop
282 s.endTop = true
283 return stateEndTop(s, c)
284 }
285 if isSpace(c) {
286 s.step = stateEndValue
287 return scanSkipSpace
288 }
289 ps := s.parseState[n-1]
290 switch ps {
291 case parseObjectKey:
292 if c == ':' {
293 s.parseState[n-1] = parseObjectValue
294 s.step = stateBeginValue
295 return scanObjectKey
296 }
297 return s.error(c, "after object key")
298 case parseObjectValue:
299 if c == ',' {
300 s.parseState[n-1] = parseObjectKey
301 s.step = stateBeginString
302 return scanObjectValue
303 }
304 if c == '}' {
305 s.popParseState()
306 return scanEndObject
307 }
308 return s.error(c, "after object key:value pair")
309 case parseArrayValue:
310 if c == ',' {
311 s.step = stateBeginValue
312 return scanArrayValue
313 }
314 if c == ']' {
315 s.popParseState()
316 return scanEndArray
317 }
318 return s.error(c, "after array element")
319 }
320 return s.error(c, "")
321 }
322
323
324
325
326 func stateEndTop(s *scanner, c byte) int {
327 if !isSpace(c) {
328
329 s.error(c, "after top-level value")
330 }
331 return scanEnd
332 }
333
334
335 func stateInString(s *scanner, c byte) int {
336 if c == '"' {
337 s.step = stateEndValue
338 return scanContinue
339 }
340 if c == '\\' {
341 s.step = stateInStringEsc
342 return scanContinue
343 }
344 if c < 0x20 {
345 return s.error(c, "in string literal")
346 }
347 return scanContinue
348 }
349
350
351 func stateInStringEsc(s *scanner, c byte) int {
352 switch c {
353 case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
354 s.step = stateInString
355 return scanContinue
356 case 'u':
357 s.step = stateInStringEscU
358 return scanContinue
359 }
360 return s.error(c, "in string escape code")
361 }
362
363
364 func stateInStringEscU(s *scanner, c byte) int {
365 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
366 s.step = stateInStringEscU1
367 return scanContinue
368 }
369
370 return s.error(c, "in \\u hexadecimal character escape")
371 }
372
373
374 func stateInStringEscU1(s *scanner, c byte) int {
375 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
376 s.step = stateInStringEscU12
377 return scanContinue
378 }
379
380 return s.error(c, "in \\u hexadecimal character escape")
381 }
382
383
384 func stateInStringEscU12(s *scanner, c byte) int {
385 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
386 s.step = stateInStringEscU123
387 return scanContinue
388 }
389
390 return s.error(c, "in \\u hexadecimal character escape")
391 }
392
393
394 func stateInStringEscU123(s *scanner, c byte) int {
395 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
396 s.step = stateInString
397 return scanContinue
398 }
399
400 return s.error(c, "in \\u hexadecimal character escape")
401 }
402
403
404 func stateNeg(s *scanner, c byte) int {
405 if c == '0' {
406 s.step = state0
407 return scanContinue
408 }
409 if '1' <= c && c <= '9' {
410 s.step = state1
411 return scanContinue
412 }
413 return s.error(c, "in numeric literal")
414 }
415
416
417
418 func state1(s *scanner, c byte) int {
419 if '0' <= c && c <= '9' {
420 s.step = state1
421 return scanContinue
422 }
423 return state0(s, c)
424 }
425
426
427 func state0(s *scanner, c byte) int {
428 if c == '.' {
429 s.step = stateDot
430 return scanContinue
431 }
432 if c == 'e' || c == 'E' {
433 s.step = stateE
434 return scanContinue
435 }
436 return stateEndValue(s, c)
437 }
438
439
440
441 func stateDot(s *scanner, c byte) int {
442 if '0' <= c && c <= '9' {
443 s.step = stateDot0
444 return scanContinue
445 }
446 return s.error(c, "after decimal point in numeric literal")
447 }
448
449
450
451 func stateDot0(s *scanner, c byte) int {
452 if '0' <= c && c <= '9' {
453 return scanContinue
454 }
455 if c == 'e' || c == 'E' {
456 s.step = stateE
457 return scanContinue
458 }
459 return stateEndValue(s, c)
460 }
461
462
463
464 func stateE(s *scanner, c byte) int {
465 if c == '+' || c == '-' {
466 s.step = stateESign
467 return scanContinue
468 }
469 return stateESign(s, c)
470 }
471
472
473
474 func stateESign(s *scanner, c byte) int {
475 if '0' <= c && c <= '9' {
476 s.step = stateE0
477 return scanContinue
478 }
479 return s.error(c, "in exponent of numeric literal")
480 }
481
482
483
484
485 func stateE0(s *scanner, c byte) int {
486 if '0' <= c && c <= '9' {
487 return scanContinue
488 }
489 return stateEndValue(s, c)
490 }
491
492
493 func stateT(s *scanner, c byte) int {
494 if c == 'r' {
495 s.step = stateTr
496 return scanContinue
497 }
498 return s.error(c, "in literal true (expecting 'r')")
499 }
500
501
502 func stateTr(s *scanner, c byte) int {
503 if c == 'u' {
504 s.step = stateTru
505 return scanContinue
506 }
507 return s.error(c, "in literal true (expecting 'u')")
508 }
509
510
511 func stateTru(s *scanner, c byte) int {
512 if c == 'e' {
513 s.step = stateEndValue
514 return scanContinue
515 }
516 return s.error(c, "in literal true (expecting 'e')")
517 }
518
519
520 func stateF(s *scanner, c byte) int {
521 if c == 'a' {
522 s.step = stateFa
523 return scanContinue
524 }
525 return s.error(c, "in literal false (expecting 'a')")
526 }
527
528
529 func stateFa(s *scanner, c byte) int {
530 if c == 'l' {
531 s.step = stateFal
532 return scanContinue
533 }
534 return s.error(c, "in literal false (expecting 'l')")
535 }
536
537
538 func stateFal(s *scanner, c byte) int {
539 if c == 's' {
540 s.step = stateFals
541 return scanContinue
542 }
543 return s.error(c, "in literal false (expecting 's')")
544 }
545
546
547 func stateFals(s *scanner, c byte) int {
548 if c == 'e' {
549 s.step = stateEndValue
550 return scanContinue
551 }
552 return s.error(c, "in literal false (expecting 'e')")
553 }
554
555
556 func stateN(s *scanner, c byte) int {
557 if c == 'u' {
558 s.step = stateNu
559 return scanContinue
560 }
561 return s.error(c, "in literal null (expecting 'u')")
562 }
563
564
565 func stateNu(s *scanner, c byte) int {
566 if c == 'l' {
567 s.step = stateNul
568 return scanContinue
569 }
570 return s.error(c, "in literal null (expecting 'l')")
571 }
572
573
574 func stateNul(s *scanner, c byte) int {
575 if c == 'l' {
576 s.step = stateEndValue
577 return scanContinue
578 }
579 return s.error(c, "in literal null (expecting 'l')")
580 }
581
582
583
584 func stateError(s *scanner, c byte) int {
585 return scanError
586 }
587
588
589 func (s *scanner) error(c byte, context string) int {
590 s.step = stateError
591 s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes}
592 return scanError
593 }
594
595
596 func quoteChar(c byte) string {
597
598 if c == '\'' {
599 return `'\''`
600 }
601 if c == '"' {
602 return `'"'`
603 }
604
605
606 s := strconv.Quote(string(c))
607 return "'" + s[1:len(s)-1] + "'"
608 }
609
View as plain text