-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfix_busted_json.py
711 lines (613 loc) · 24 KB
/
fix_busted_json.py
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#!/usr/bin/env python3
import json
import re
class JsonFixError(Exception):
pass
def log(obj):
if isinstance(obj, (int, float)):
print(obj)
elif isinstance(obj, bool):
print(obj)
elif isinstance(obj, dict):
log_pretty(obj)
print()
else:
log_jsons(obj)
def log_jsons(text):
array = to_array_of_plain_strings_or_json(text)
for item in array:
log_pretty(item)
if is_json(item):
log_jsons_in_json(item)
print()
def log_jsons_in_json(item):
obj = json.loads(item)
for key in obj:
if obj[key] and isinstance(obj[key], dict):
log_jsons_in_json(json.dumps(obj[key]))
elif can_parse_json(obj[key]):
print(f"\nFOUND JSON found in key {key} --->")
log_jsons(obj[key])
def is_json(text):
try:
result = json.loads(text)
if not result:
return False
if not isinstance(result, dict):
return False
return True
except Exception:
return False
def log_pretty(obj):
try:
if isinstance(obj, str):
json_obj = json.loads(obj)
else:
json_obj = obj
print(json.dumps(json_obj, indent=2))
except Exception:
print(obj)
def repair_json(input):
parse_json = JsonParser(input)
return parse_json.repair_json()
def to_array_of_plain_strings_or_json(input):
parse_json = JsonParser(input)
return parse_json.to_array_of_plain_strings_or_json()
def can_parse_json(input):
parse_json = JsonParser(input)
try:
parse_json.repair_json()
return True
except Exception:
return False
def first_json(input):
parse_json = JsonParser(input)
result = parse_json.to_array_of_plain_strings_or_json()
for item in result:
if can_parse_json(item):
return item
return ""
def last_json(input):
parse_json = JsonParser(input)
result = parse_json.to_array_of_plain_strings_or_json()
for i in range(len(result) - 1, -1, -1):
if can_parse_json(result[i]):
return result[i]
return ""
def largest_json(input):
parse_json = JsonParser(input)
result = parse_json.to_array_of_plain_strings_or_json()
largest = ""
for item in result:
if can_parse_json(item) and len(item) > len(largest):
largest = item
return largest
def json_matching(input, regex):
parse_json = JsonParser(input)
result = parse_json.to_array_of_plain_strings_or_json()
for item in result:
if can_parse_json(item) and regex.search(item):
return item
return ""
class JsonParser:
def __init__(self, input):
self.inspected = self.de_stringify(input)
self.reset_pointer()
self.debug = False
def reset_pointer(self):
self.position = 0
self.quoted = ''
self.checkpoint = 0
self.checkpoint_quoted = ''
self.quoted_last_comma_position = None
def set_checkpoint(self):
if self.debug:
print('setCheckpoint', self.position, self.inspected[self.position])
self.checkpoint = self.position
self.checkpoint_quoted = self.quoted
def repair_json(self):
self.reset_pointer()
self.eat_object()
return self.quoted
def de_stringify(self, string):
try:
result = json.loads(string)
if isinstance(result, str):
return self.de_stringify(result)
return string
except Exception as e:
return string
def to_array_of_plain_strings_or_json(self):
result = []
self.reset_pointer()
recovery_position = 0
while self.position < len(self.inspected):
self.quoted = ''
self.eat_plain_text()
result.append(self.quoted)
self.quoted = ''
if self.position >= len(self.inspected):
break
if self.inspected[self.position] == '{':
recovery_position = self.position + 1
try:
self.eat_object()
except Exception as e:
self.quoted += '{'
self.position = recovery_position
result.append(self.quoted)
return result
def eat_plain_text(self):
while self.position < len(self.inspected) and self.inspected[self.position] != '{':
if self.debug:
print('eat_plain_text', self.position, self.inspected[self.position])
self.quoted += self.inspected[self.position]
self.position += 1
def eat_object(self):
if self.debug:
print('eat_object', self.position, self.inspected[self.position])
self.eat_whitespace()
self.eat_open_brace()
self.eat_key_value_pairs()
self.eat_whitespace()
self.eat_close_brace()
def eat_key_value_pairs(self):
if self.debug:
print('eat_key_value_pairs', self.position, self.inspected[self.position])
while True:
self.eat_whitespace()
if self.inspected[self.position] == '}':
self.remove_trailing_comma_if_present()
break
self.quoted_last_comma_position = None
self.eat_key()
self.eat_whitespace()
self.eat_colon()
self.eat_whitespace()
self.eat_reference_optional()
self.eat_whitespace()
self.eat_value()
self.quoted_last_comma_position = None
self.eat_whitespace()
if self.inspected[self.position] == ',':
self.eat_comma()
elif self.inspected[self.position] != '}':
self.quoted += ', '
def eat_reference_optional(self):
if self.inspected[self.position] == '<':
self.eat_reference()
def eat_reference(self):
self.set_checkpoint()
self.eat_open_angle_bracket()
self.eat_whitespace()
self.eat_ref()
self.eat_whitespace()
self.eat_asterisk()
self.eat_whitespace()
self.eat_reference_number()
self.eat_whitespace()
self.eat_close_angle_bracket()
def eat_open_angle_bracket(self):
if self.inspected[self.position] != '<':
raise JsonFixError('Expected open angle bracket')
self.position += 1
def eat_ref(self):
if self.inspected[self.position] != 'r':
raise JsonFixError('Expected r')
self.position += 1
if self.inspected[self.position] != 'e':
raise JsonFixError('Expected e')
self.position += 1
if self.inspected[self.position] != 'f':
raise JsonFixError('Expected f')
self.position += 1
def eat_asterisk(self):
if self.inspected[self.position] != '*':
raise JsonFixError('Expected asterisk')
self.position += 1
def eat_reference_number(self):
number_regex = re.compile(r'[0-9]')
while number_regex.match(self.inspected[self.position]):
self.position += 1
def eat_close_angle_bracket(self):
if self.inspected[self.position] != '>':
raise JsonFixError('Expected close angle bracket')
self.position += 1
def eat_comma_post_value_optional(self):
if self.inspected[self.position] == ',':
self.eat_comma()
return True
return False
def eat_whitespace(self):
whitespace_regex = re.compile(r'\s')
while whitespace_regex.match(self.inspected[self.position]):
self.position += 1
def eat_open_brace(self):
if self.debug:
print('eat_open_brace', self.position, self.inspected[self.position])
if self.inspected[self.position] != '{':
raise JsonFixError('Expected open brace')
self.quoted += self.inspected[self.position] + ' '
self.position += 1
def eat_close_brace(self):
if self.debug:
print('eat_close_brace', self.position, self.inspected[self.position])
if self.inspected[self.position] != '}':
raise JsonFixError('Expected close brace')
self.quoted += ' ' + self.inspected[self.position]
self.position += 1
def eat_key(self):
if self.debug:
print('eat_key', self.position, self.inspected[self.position])
if self.get_quote():
self.eat_quoted_key()
else:
self.eat_unquoted_key()
def get_quote(self):
if self.inspected[self.position] == "'":
return "'"
if self.inspected[self.position] == '"':
return '"'
if self.inspected[self.position] == '`':
return '`'
if self.inspected[self.position] == '“':
return '”'
if self.inspected[self.position] == '\\' and self.inspected[self.position + 1] == '"':
return '\\"'
if (
self.inspected[self.position] == '\\' and
self.inspected[self.position + 1] == '\\' and
self.inspected[self.position + 2] == '"'
):
return '\\\\"'
return False
def check_quote(self, quote):
if len(quote) == 1:
return self.inspected[self.position] == quote
if len(quote) == 2:
return (
self.inspected[self.position] == quote[0] and
self.inspected[self.position + 1] == quote[1]
)
if len(quote) == 3:
return (
self.inspected[self.position] == quote[0] and
self.inspected[self.position + 1] == quote[1] and
self.inspected[self.position + 2] == quote[2]
)
return False
def eat_long_quote(self, quote):
eat_extra = len(quote) - 1
for _ in range(eat_extra):
self.position += 1
def eat_extra_starting_key_double_quote(self, quote):
if quote != '"':
return
if self.inspected[self.position] == '"':
virtual_position = self.eat_virtual_whitespace(self.position + 1)
if self.inspected[virtual_position] == ':':
return
self.log('eatExtraStartingKeyDoubleQuote')
self.position += 1
def eat_quoted_key(self):
self.log('eatQuotedKey')
self.set_checkpoint()
self.throw_if_json_special_character(self.inspected[self.position])
quote = self.get_quote()
self.quoted += '"'
self.position += 1
self.eat_long_quote(quote)
self.eat_extra_starting_key_double_quote(quote)
while not self.check_quote(quote):
self.eat_char_or_escaped_char(quote)
self.log('eatQuotedKey end')
self.quoted += '"'
self.position += 1
self.eat_long_quote(quote)
def eat_unquoted_key(self):
if self.debug:
print('eat_unquoted_key', self.position, self.inspected[self.position])
self.set_checkpoint()
if self.inspected[self.position] == '[':
return self.eat_null_key()
self.throw_if_json_special_character(self.inspected[self.position])
self.quoted += '"'
while self.inspected[self.position] != ':' and self.inspected[self.position] != ' ':
if self.get_quote():
raise JsonFixError('Unexpected quote in unquoted key')
self.quoted += self.inspected[self.position]
self.position += 1
self.quoted += '"'
def eat_null_key(self):
if self.debug:
print('eat_null_key', self.position, self.inspected[self.position])
if self.inspected[self.position] != '[':
raise JsonFixError('Expected open bracket')
self.position += 1
if self.inspected[self.position].lower() != 'n':
raise JsonFixError('Expected n')
self.position += 1
if self.inspected[self.position].lower() != 'u':
raise JsonFixError('Expected u')
self.position += 1
if self.inspected[self.position].lower() != 'l':
raise JsonFixError('Expected l')
self.position += 1
if self.inspected[self.position].lower() != 'l':
raise JsonFixError('Expected l')
self.position += 1
if self.inspected[self.position] != ']':
raise JsonFixError('Expected close bracket')
self.position += 1
self.quoted += '"null"'
def throw_if_json_special_character(self, char):
if char in ['{', '}', '[', ']', ':', ',']:
raise JsonFixError(f'Unexpected character {char} at position {self.position}')
def eat_colon(self):
if self.debug:
print('eat_colon', self.position, self.inspected[self.position])
if self.inspected[self.position] != ':':
raise JsonFixError('Expected colon')
self.quoted += self.inspected[self.position] + ' '
self.position += 1
def eat_value(self):
if self.debug:
print('eat_value', self.position, self.inspected[self.position])
if self.inspected[self.position] == '{':
self.eat_object()
elif self.get_quote():
self.eat_string()
self.eat_concatenated_strings()
elif self.inspected[self.position] == '[':
self.eat_array()
else:
self.eat_primitive()
def eat_string(self):
if self.debug:
print('eat_string', self.position, self.inspected[self.position])
self.set_checkpoint()
quote = self.get_quote()
self.quoted += '"'
self.position += 1
self.eat_long_quote(quote)
while not self.is_end_quote_making_allowance_for_unescaped_single_quote(quote):
self.eat_char_or_escaped_char(quote)
self.quoted += '"'
self.position += 1
self.eat_long_quote(quote)
def eat_concatenated_strings(self):
if self.debug:
print('eat_concatenated_strings', self.position, self.inspected[self.position])
virtual_position = self.eat_virtual_whitespace(self.position + 1)
if self.inspected[virtual_position] != '+':
return
self.position = virtual_position + 1
self.eat_whitespace()
self.quoted = self.quoted[:-1]
quote = self.get_quote()
self.position += 1
self.eat_long_quote(quote)
while not self.is_end_quote_making_allowance_for_unescaped_single_quote(quote):
self.eat_char_or_escaped_char(quote)
self.quoted += '"'
self.position += 1
self.eat_long_quote(quote)
self.eat_concatenated_strings()
def is_end_quote_making_allowance_for_unescaped_single_quote(self, quote):
if quote != "'":
return self.check_quote(quote)
try:
if self.check_quote(quote) and self.inspected[self.position + 1] == 's':
return False
except IndexError:
pass
return self.check_quote(quote)
def eat_virtual_whitespace(self, virtual_position):
if virtual_position >= len(self.inspected):
return virtual_position - 1
whitespace_regex = re.compile(r'\s')
while virtual_position < len(self.inspected) and whitespace_regex.match(self.inspected[virtual_position]):
virtual_position += 1
return virtual_position
def is_double_escaped_double_quote(self):
if self.position + 2 >= len(self.inspected):
return False
return (
self.inspected[self.position] == '\\' and
self.inspected[self.position + 1] == '\\' and
self.inspected[self.position + 2] == '"'
)
def is_triple_escaped_double_quote(self):
if self.position + 3 >= len(self.inspected):
return False
return (
self.inspected[self.position] == '\\' and
self.inspected[self.position + 1] == '\\' and
self.inspected[self.position + 2] == '\\' and
self.inspected[self.position + 3] == '"'
)
def eat_char_or_escaped_char(self, quote):
if self.debug:
print('eat_char_or_escaped_char', self.position, self.inspected[self.position])
if self.position >= len(self.inspected):
raise JsonFixError('Unexpected end of quoted key or string')
if self.debug:
print(
'eatCharOrEscapedChar',
self.position,
self.inspected[self.position],
' ' + str(ord(self.inspected[self.position])),
)
if not self.check_quote(quote) and self.inspected[self.position] == '\\':
if self.is_triple_escaped_double_quote():
self.log('eatCharOrEscapedChar triple escaped double quote')
self.position += 1
self.position += 1
if self.is_double_escaped_double_quote():
self.log('eatCharOrEscapedChar double escaped double quote')
self.position += 1
if (quote == "'" or quote == '`') and self.inspected[self.position + 1] == quote:
pass
else:
self.quoted += self.inspected[self.position]
self.position += 1
if (quote == "'" or quote == '`') and self.inspected[self.position] == '"':
self.quoted += '\\'
if (self.inspected[self.position] == '\n'):
self.quoted += '\\n'
self.log('eatCharOrEscapedChar unescaped newline')
else:
self.quoted += self.inspected[self.position]
self.position += 1
def eat_array(self):
if self.debug:
print('eat_array', self.position, self.inspected[self.position])
if self.inspected[self.position] != '[':
raise JsonFixError('Expected array')
self.quoted += self.inspected[self.position]
self.position += 1
while True:
self.eat_whitespace()
self.eat_circular_optional()
if self.inspected[self.position] == ']':
self.remove_trailing_comma_if_present()
break
self.quoted_last_comma_position = None
self.eat_value()
self.eat_whitespace()
if self.inspected[self.position] == ',':
self.eat_comma()
elif self.inspected[self.position] != ']':
self.quoted += ', '
self.eat_close_bracket()
def remove_trailing_comma_if_present(self):
if self.quoted_last_comma_position:
self.quoted = (
self.quoted[:self.quoted_last_comma_position] +
self.quoted[self.quoted_last_comma_position + 2:]
)
self.quoted_last_comma_position = None
def eat_circular_optional(self):
if (
self.inspected[self.position] == 'C' and
self.inspected[self.position + 1] == 'i' and
self.inspected[self.position + 2] == 'r' and
self.inspected[self.position + 3] == 'c' and
self.inspected[self.position + 4] == 'u' and
self.inspected[self.position + 5] == 'l' and
self.inspected[self.position + 6] == 'a' and
self.inspected[self.position + 7] == 'r'
):
self.eat_circular()
def eat_circular(self):
test_regex = re.compile(r'[Circular *\d]')
while test_regex.match(self.inspected[self.position]):
self.position += 1
self.quoted += '"Circular"'
def eat_comma(self):
if self.debug:
print('eat_comma', self.position, self.inspected[self.position])
if self.inspected[self.position] != ',':
raise JsonFixError('Expected comma')
self.quoted += self.inspected[self.position] + ' '
self.quoted_last_comma_position = len(self.quoted) - 2
self.position += 1
return True
def eat_close_bracket(self):
if self.inspected[self.position] != ']':
raise JsonFixError('Expected close bracket')
self.quoted += self.inspected[self.position]
self.position += 1
return False
# def eat_primitive(self):
# self.set_checkpoint()
# if (
# self.inspected[self.position].lower() == 'f' and
# self.inspected[self.position + 1].lower() == 'a' and
# self.inspected[self.position + 2].lower() == 'l' and
# self.inspected[self.position + 3].lower() == 's' and
# self.inspected[self.position + 4].lower() == 'e'
# ):
# return self.eat_false()
# if (
# self.inspected[self.position].lower() == 'n' and
# self.inspected[self.position + 1].lower() == 'o' and
# self.inspected[self.position + 2].lower() == 'n' and
# self.inspected[self.position + 3].lower() == 'e'
# ):
# return self.eat_none()
# if (
# self.inspected[self.position].lower() == 't' and
# self.inspected[self.position + 1].lower() == 'r' and
# self.inspected[self.position + 2].lower() == 'u' and
# self.inspected[self.position + 3].lower() == 'e'
# ):
# return self.eat_true()
# primitive_regex = re.compile(r'([0-9a-zA-Z-.])')
# while primitive_regex.match(self.inspected[self.position]):
# self.quoted += self.inspected[self.position]
# self.position += 1
# def eat_false(self):
# self.quoted += 'false'
# self.position += 5
# def eat_none(self):
# self.quoted += 'null'
# self.position += 4
# def eat_true(self):
# self.quoted += 'true'
# self.position += 4
def eat_primitive(self):
self.set_checkpoint()
if self.debug:
print('eatPrimitive', self.position, self.inspected[self.position])
lower_char = self.inspected[self.position].lower()
if lower_char == 'f' or lower_char == 't' or lower_char == 'n':
self.eat_keyword()
elif self.is_number_start_char(lower_char):
self.eat_number()
else:
raise ValueError('Primitive not recognized, must start with f, t, n, or be numeric')
def is_number_start_char(self, char):
return char and re.match(r'[\-0-9]', char)
def eat_keyword(self):
lower_substring = self.inspected[self.position:self.position + 5].lower()
if lower_substring.startswith('false'):
self.log('eatFalse')
self.quoted += 'false'
self.position += 5
elif lower_substring.startswith('true'):
self.log('eatTrue')
self.quoted += 'true'
self.position += 4
elif lower_substring.startswith('none') or lower_substring.startswith('null'):
self.log('eatNull')
self.quoted += 'null'
self.position += 4
else:
raise ValueError('Keyword not recognized, must be true, false, null or none')
def eat_number(self):
number_str = ''
self.log('eatNumber')
while self.is_number_char(self.inspected[self.position]):
number_str += self.inspected[self.position]
self.position += 1
number_str = number_str.lower()
check_str = number_str
if check_str.startswith('-'):
check_str = check_str[1:]
if len(check_str) > 1 and check_str.startswith('0') and not check_str.startswith('0.'):
raise ValueError('Number cannot have redundant leading 0')
if check_str.endswith('.'):
raise ValueError('Number cannot have trailing decimal point')
if '.e' in check_str or '.E' in check_str:
raise ValueError('Number cannot have decimal point followed by exponent')
if check_str.endswith('e') or check_str.endswith('E'):
raise ValueError('Number cannot have trailing exponent')
if check_str.endswith('-') or check_str.endswith('+'):
raise ValueError('Number cannot have trailing sign')
self.quoted += number_str
def is_number_char(self, char):
return char and re.match(r'[\-\+eE0-9.]', char)
def log(self, message):
if self.debug:
print(message, self.position, self.inspected[self.position])