@@ -101,29 +101,35 @@ type JSONParser struct {
101
101
// receiver p
102
102
// return any
103
103
func (p * JSONParser ) parseJSON () any {
104
- c , b := p .getByte (0 )
105
- if ! b {
106
- return ""
107
- }
108
104
109
- switch {
110
- case c == '{' :
111
- p .index ++
112
- return p .parseObject ()
113
- case c == '[' :
105
+ for {
106
+ c , b := p .getByte (0 )
107
+
108
+ if ! b {
109
+ return ""
110
+ }
111
+
112
+ isInMarkers := len (p .marker ) > 0
113
+
114
+ switch {
115
+ case c == '{' :
116
+ p .index ++
117
+ return p .parseObject ()
118
+ case c == '[' :
119
+ p .index ++
120
+ return p .parseArray ()
121
+ case c == '}' :
122
+ return ""
123
+ // TODO Full-width character support
124
+ case isInMarkers && (bytes .IndexByte ([]byte {'"' , '\'' }, c ) != - 1 || unicode .IsLetter (rune (c ))):
125
+ return p .parseString ()
126
+ case isInMarkers && (unicode .IsNumber (rune (c )) || bytes .IndexByte ([]byte {'-' , '.' }, c ) != - 1 ):
127
+ return p .parseNumber ()
128
+ }
129
+
114
130
p .index ++
115
- return p .parseArray ()
116
- case c == '}' :
117
- return ""
118
- // TODO Full-width character support
119
- case bytes .IndexByte ([]byte {'"' , '\'' }, c ) != - 1 || unicode .IsLetter (rune (c )):
120
- return p .parseString ()
121
- case unicode .IsNumber (rune (c )) || bytes .IndexByte ([]byte {'-' , '.' }, c ) != - 1 :
122
- return p .parseNumber ()
123
131
}
124
132
125
- p .index ++
126
- return p .parseJSON ()
127
133
}
128
134
129
135
// parseObject
@@ -166,11 +172,15 @@ func (p *JSONParser) parseObject() map[string]any {
166
172
}
167
173
}
168
174
175
+ p .skipWhitespaces ()
176
+
169
177
c , b = p .getByte (0 )
170
178
if b && c == '}' {
171
179
continue
172
180
}
173
181
182
+ p .skipWhitespaces ()
183
+
174
184
c , b = p .getByte (0 )
175
185
//nolint
176
186
if ! b || c != ':' {
@@ -226,7 +236,7 @@ func (p *JSONParser) parseArray() []any {
226
236
p .skipWhitespaces ()
227
237
value := p .parseJSON ()
228
238
229
- if value == nil {
239
+ if value == nil || value == "" {
230
240
break
231
241
}
232
242
@@ -307,11 +317,6 @@ func (p *JSONParser) parseString() any {
307
317
}
308
318
}
309
319
310
- if p .getMarker () == "" {
311
- p .index ++
312
- return p .parseJSON ()
313
- }
314
-
315
320
missingQuotes = true
316
321
}
317
322
@@ -333,7 +338,17 @@ func (p *JSONParser) parseString() any {
333
338
if nextB && b && c == rStringDelimiter {
334
339
doubledQuotes = true
335
340
p .index ++
341
+ } else {
342
+ i = 1
343
+ nextC , nextB = p .getByte (i )
344
+ for nextB && nextC == ' ' {
345
+ i ++
346
+ nextC , nextB = p .getByte (i )
347
+ }
336
348
349
+ if nextB && bytes .IndexByte ([]byte {',' , ']' , '}' }, nextC ) == - 1 {
350
+ p .index ++
351
+ }
337
352
}
338
353
}
339
354
@@ -346,7 +361,33 @@ func (p *JSONParser) parseString() any {
346
361
if p .getMarker () == "object_key" && (c == ':' || unicode .IsSpace (rune (c ))) {
347
362
break
348
363
} else if p .getMarker () == "object_value" && bytes .IndexByte ([]byte {',' , '}' }, c ) != - 1 {
349
- break
364
+
365
+ rStringDelimiterMissing := true
366
+ i := 1
367
+ nextC , nextB := p .getByte (i )
368
+ for nextB && nextC != rStringDelimiter {
369
+ i ++
370
+ nextC , nextB = p .getByte (i )
371
+ }
372
+
373
+ if nextB {
374
+ i ++
375
+ nextC , nextB = p .getByte (i )
376
+ }
377
+
378
+ for nextB && nextC == ' ' {
379
+ i ++
380
+ nextC , nextB = p .getByte (i )
381
+ }
382
+
383
+ if nextB && bytes .IndexByte ([]byte {',' , '}' }, nextC ) != - 1 {
384
+ rStringDelimiterMissing = false
385
+ }
386
+
387
+ if rStringDelimiterMissing {
388
+ break
389
+ }
390
+
350
391
}
351
392
}
352
393
@@ -383,24 +424,53 @@ func (p *JSONParser) parseString() any {
383
424
384
425
if doubledQuotes && p .container [p .index + 1 ] == rStringDelimiter {
385
426
427
+ } else if missingQuotes && p .getMarker () == "object_value" {
428
+
429
+ i := 1
430
+ nextC , nextB := p .getByte (i )
431
+ for nextB && bytes .IndexByte ([]byte {rStringDelimiter , lStringDelimiter }, nextC ) == - 1 {
432
+ i ++
433
+ nextC , nextB = p .getByte (i )
434
+ }
435
+
436
+ if nextB {
437
+ i ++
438
+ nextC , nextB = p .getByte (i )
439
+ for nextB && nextC == ' ' {
440
+ i ++
441
+ nextC , nextB = p .getByte (i )
442
+ }
443
+
444
+ if nextB && nextC == ':' {
445
+ p .index --
446
+ c , b = p .getByte (0 )
447
+ break
448
+ }
449
+ }
450
+
386
451
} else {
387
452
388
453
i := 1
389
454
nextC , nextB := p .getByte (i )
390
- for nextB && nextC != rStringDelimiter {
455
+ checkCommaInObjectValue := true
456
+ for nextB && bytes .IndexByte ([]byte {rStringDelimiter , lStringDelimiter }, nextC ) == - 1 {
391
457
392
- if nextC == lStringDelimiter ||
393
- (slices .Contains (p .marker , "object_key" ) && nextC == ':' ) ||
394
- (slices .Contains (p .marker , "object_value" ) && bytes .IndexByte ([]byte {'}' , ',' }, nextC ) != - 1 ) ||
395
- (slices .Contains (p .marker , "array" ) && bytes .IndexByte ([]byte {']' , ',' }, nextC ) != - 1 ) {
458
+ if unicode .IsLetter (rune (c )) {
459
+ checkCommaInObjectValue = false
460
+ }
461
+
462
+ if (slices .Contains (p .marker , "object_key" ) && bytes .IndexByte ([]byte {':' , '}' }, nextC ) != - 1 ) ||
463
+ (slices .Contains (p .marker , "object_value" ) && nextC == '}' ) ||
464
+ (slices .Contains (p .marker , "array" ) && bytes .IndexByte ([]byte {']' , ',' }, nextC ) != - 1 ) ||
465
+ (checkCommaInObjectValue && p .getMarker () == "object_value" && nextC == ',' ) {
396
466
break
397
467
}
398
468
399
469
i ++
400
470
nextC , nextB = p .getByte (i )
401
471
}
402
472
403
- if nextC == rStringDelimiter {
473
+ if nextC == ',' && p . getMarker () == "object_value" {
404
474
i ++
405
475
nextC , nextB = p .getByte (i )
406
476
for nextB && nextC != rStringDelimiter {
@@ -410,20 +480,43 @@ func (p *JSONParser) parseString() any {
410
480
i ++
411
481
nextC , nextB = p .getByte (i )
412
482
413
- for nextB && nextC != ':' {
414
- if bytes .IndexByte ([]byte {lStringDelimiter , rStringDelimiter , ',' }, nextC ) != - 1 {
415
- break
416
- }
483
+ for nextB && nextC == ' ' {
417
484
i ++
418
485
nextC , nextB = p .getByte (i )
419
486
}
420
487
421
- // upstream
422
- if ! nextB || nextC != ':' {
488
+ if nextB && nextC == '}' {
423
489
rst = append (rst , c )
424
490
p .index ++
425
491
c , b = p .getByte (0 )
426
492
}
493
+ } else if nextB && nextC == rStringDelimiter {
494
+
495
+ if p .getMarker () == "object_value" {
496
+ i ++
497
+ nextC , nextB = p .getByte (i )
498
+ for nextB && nextC != rStringDelimiter {
499
+ i ++
500
+ nextC , nextB = p .getByte (i )
501
+ }
502
+ i ++
503
+ nextC , nextB = p .getByte (i )
504
+ for nextB && nextC != ':' {
505
+ if bytes .IndexByte ([]byte {',' , lStringDelimiter , rStringDelimiter }, nextC ) != - 1 {
506
+ break
507
+ }
508
+ i ++
509
+ nextC , nextB = p .getByte (i )
510
+ }
511
+
512
+ if nextC != ':' {
513
+ rst = append (rst , c )
514
+ p .index ++
515
+ c , b = p .getByte (0 )
516
+ }
517
+
518
+ }
519
+
427
520
}
428
521
}
429
522
}
@@ -462,7 +555,10 @@ func (p *JSONParser) parseNumber() any {
462
555
463
556
c , b = p .getByte (0 )
464
557
465
- for b && bytes .IndexByte (numberChars , c ) != - 1 {
558
+ isArray := p .getMarker () == "array"
559
+
560
+ for b && bytes .IndexByte (numberChars , c ) != - 1 &&
561
+ (c != ',' || ! isArray ) {
466
562
rst = append (rst , c )
467
563
p .index ++
468
564
c , b = p .getByte (0 )
0 commit comments