@@ -22,22 +22,22 @@ def __init__(self, data=b"", chunk_size=10):
22
22
self .chunks_read = 0
23
23
self .data = data
24
24
self .data_len = len (self .data )
25
- self .poition = 0
25
+ self .position = 0
26
26
27
27
def __iter__ (self ):
28
28
return self
29
29
30
30
def __next__ (self ):
31
- if self .poition > self .data_len :
31
+ if self .position > self .data_len :
32
32
raise StopIteration
33
33
34
34
end = self .chunk_size
35
- if self .poition + end > self .data_len :
35
+ if self .position + end > self .data_len :
36
36
end = self .data_len
37
- chunk = self .data [self .poition : self .poition + self .chunk_size ]
37
+ chunk = self .data [self .position : self .position + self .chunk_size ]
38
38
39
39
self .chunks_read += 1
40
- self .poition += self .chunk_size
40
+ self .position += self .chunk_size
41
41
42
42
return chunk
43
43
@@ -54,6 +54,7 @@ def get_chunks_read(self):
54
54
def dict_with_all_types ():
55
55
return """
56
56
{
57
+ "_check": "{\\ \" a\\ \" : 1, \\ \" b\\ \" : [2,3]}",
57
58
"bool": true,
58
59
"dict": {"key": "value"},
59
60
"float": 1.1,
@@ -341,6 +342,28 @@ def test_complex_dict_passed_key_raises(complex_dict):
341
342
stream ["obects_id" ]
342
343
343
344
345
+ def test_complex_dict_passed_reference_raises (complex_dict ):
346
+ """
347
+ Test loading a complex dict and attempting to grab a data from a saved reference that has
348
+ been passed raises.
349
+ """
350
+
351
+ assert json .loads (complex_dict )
352
+
353
+ stream = adafruit_json_stream .load (BytesChunkIO (complex_dict .encode ()))
354
+
355
+ list_1 = stream ["list_1" ]
356
+ dict_1 = next (list_1 )
357
+ sub_dict = dict_1 ["sub_dict" ]
358
+ sub_list = dict_1 ["sub_list" ]
359
+ list_2 = stream ["list_2" ]
360
+ next (list_2 )
361
+ with pytest .raises (KeyError , match = "sub_dict_id" ):
362
+ sub_dict ["sub_dict_id" ]
363
+ with pytest .raises (StopIteration ):
364
+ next (sub_list )
365
+
366
+
344
367
# complex_dict is 1518 bytes
345
368
@pytest .mark .parametrize (
346
369
("chunk_size" , "expected_chunks" ), ((10 , 152 ), (50 , 31 ), (100 , 16 ), (5000 , 1 ))
@@ -456,7 +479,9 @@ def test_as_object_stream(dict_with_all_types):
456
479
457
480
stream = adafruit_json_stream .load (BytesChunkIO (dict_with_all_types .encode ()))
458
481
459
- assert stream .as_object () == {
482
+ obj = stream .as_object ()
483
+ assert obj == {
484
+ "_check" : '{"a": 1, "b": [2,3]}' ,
460
485
"bool" : True ,
461
486
"dict" : {"key" : "value" },
462
487
"float" : 1.1 ,
@@ -465,6 +490,13 @@ def test_as_object_stream(dict_with_all_types):
465
490
"null" : None ,
466
491
"string" : "string" ,
467
492
}
493
+ assert json .loads (obj ["_check" ]) == {
494
+ "a" : 1 ,
495
+ "b" : [
496
+ 2 ,
497
+ 3 ,
498
+ ],
499
+ }
468
500
469
501
470
502
def test_as_object_that_is_partially_read_raises (complex_dict ):
0 commit comments