@@ -66,6 +66,38 @@ def dict_with_all_types():
6666 """
6767
6868
69+ @pytest .fixture
70+ def list_with_bad_strings ():
71+ return r"""
72+ [
73+ "\"}\"",
74+ "{\"a\": 1, \"b\": [2,3]}",
75+ "\"",
76+ "\\\"",
77+ "\\\\\"",
78+ "\\x40\"",
79+ "[[[{{{",
80+ "]]]}}}"
81+ ]
82+ """
83+
84+
85+ @pytest .fixture
86+ def dict_with_bad_strings ():
87+ return r"""
88+ {
89+ "1": "\"}\"",
90+ "2": "{\"a\": 1, \"b\": [2,3]}",
91+ "3": "\"",
92+ "4": "\\\"",
93+ "5": "\\\\\"",
94+ "6": "\\x40\"",
95+ "7": "[[[{{{",
96+ "8": "]]]}}}"
97+ }
98+ """
99+
100+
69101@pytest .fixture
70102def list_with_values ():
71103 return """
@@ -308,6 +340,116 @@ def test_complex_dict(complex_dict):
308340 assert sub_counter == 12
309341
310342
343+ def test_bad_strings_in_list (list_with_bad_strings ):
344+ """Test loading different strings that can confuse the parser."""
345+
346+ bad_strings = [
347+ '"}"' ,
348+ '{"a": 1, "b": [2,3]}' ,
349+ '"' ,
350+ '\\ "' ,
351+ '\\ \\ "' ,
352+ '\\ x40"' ,
353+ "[[[{{{" ,
354+ "]]]}}}" ,
355+ ]
356+
357+ assert json .loads (list_with_bad_strings )
358+
359+ # get each separately
360+ stream = adafruit_json_stream .load (BytesChunkIO (list_with_bad_strings .encode ()))
361+ for i , item in enumerate (stream ):
362+ assert item == bad_strings [i ]
363+
364+
365+ def test_bad_strings_in_list_iter (list_with_bad_strings ):
366+ """Test loading different strings that can confuse the parser."""
367+
368+ bad_strings = [
369+ '"}"' ,
370+ '{"a": 1, "b": [2,3]}' ,
371+ '"' ,
372+ '\\ "' ,
373+ '\\ \\ "' ,
374+ '\\ x40"' ,
375+ "[[[{{{" ,
376+ "]]]}}}" ,
377+ ]
378+
379+ assert json .loads (list_with_bad_strings )
380+
381+ # get each separately
382+ stream = adafruit_json_stream .load (BytesChunkIO (list_with_bad_strings .encode ()))
383+ for i , item in enumerate (stream ):
384+ assert item == bad_strings [i ]
385+
386+
387+ def test_bad_strings_in_dict_as_object (dict_with_bad_strings ):
388+ """Test loading different strings that can confuse the parser."""
389+
390+ bad_strings = {
391+ "1" : '"}"' ,
392+ "2" : '{"a": 1, "b": [2,3]}' ,
393+ "3" : '"' ,
394+ "4" : '\\ "' ,
395+ "5" : '\\ \\ "' ,
396+ "6" : '\\ x40"' ,
397+ "7" : "[[[{{{" ,
398+ "8" : "]]]}}}" ,
399+ }
400+
401+ # read all at once
402+ stream = adafruit_json_stream .load (BytesChunkIO (dict_with_bad_strings .encode ()))
403+ assert stream .as_object () == bad_strings
404+
405+
406+ def test_bad_strings_in_dict_all_keys (dict_with_bad_strings ):
407+ """Test loading different strings that can confuse the parser."""
408+
409+ bad_strings = {
410+ "1" : '"}"' ,
411+ "2" : '{"a": 1, "b": [2,3]}' ,
412+ "3" : '"' ,
413+ "4" : '\\ "' ,
414+ "5" : '\\ \\ "' ,
415+ "6" : '\\ x40"' ,
416+ "7" : "[[[{{{" ,
417+ "8" : "]]]}}}" ,
418+ }
419+
420+ # read one after the other with keys
421+ stream = adafruit_json_stream .load (BytesChunkIO (dict_with_bad_strings .encode ()))
422+ assert stream ["1" ] == bad_strings ["1" ]
423+ assert stream ["2" ] == bad_strings ["2" ]
424+ assert stream ["3" ] == bad_strings ["3" ]
425+ assert stream ["4" ] == bad_strings ["4" ]
426+ assert stream ["5" ] == bad_strings ["5" ]
427+ assert stream ["6" ] == bad_strings ["6" ]
428+ assert stream ["7" ] == bad_strings ["7" ]
429+ assert stream ["8" ] == bad_strings ["8" ]
430+
431+
432+ def test_bad_strings_in_dict_skip_some (dict_with_bad_strings ):
433+ """Test loading different strings that can confuse the parser."""
434+
435+ bad_strings = {
436+ "1" : '"}"' ,
437+ "2" : '{"a": 1, "b": [2,3]}' ,
438+ "3" : '"' ,
439+ "4" : '\\ "' ,
440+ "5" : '\\ \\ "' ,
441+ "6" : '\\ x40"' ,
442+ "7" : "[[[{{{" ,
443+ "8" : "]]]}}}" ,
444+ }
445+
446+ # read some, skip some
447+ stream = adafruit_json_stream .load (BytesChunkIO (dict_with_bad_strings .encode ()))
448+ assert stream ["2" ] == bad_strings ["2" ]
449+ assert stream ["5" ] == bad_strings ["5" ]
450+ assert stream ["8" ] == bad_strings ["8" ]
451+
452+
311453def test_complex_dict_grabbing (complex_dict ):
312454 """Test loading a complex dict and grabbing specific keys."""
313455
0 commit comments