Skip to content

Commit 1b47d3e

Browse files
committed
Allow multiple subscriptable levels twice
1 parent 18843b5 commit 1b47d3e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

adafruit_json_stream.py

+6
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,19 @@ def __init__(self, stream):
174174
super().__init__(stream)
175175
self.finish_char = "}"
176176
self.start_char = "{"
177+
self.active_child_key = None
177178

178179
def __getitem__(self, key):
180+
if self.active_child and self.active_child_key == key:
181+
return self.active_child
182+
179183
self.has_read = True
180184

181185
if self.active_child:
182186
self.active_child.finish()
183187
self.done = self.data.fast_forward(",")
184188
self.active_child = None
189+
self.active_child_key = None
185190
if self.done:
186191
raise KeyError(key)
187192

@@ -196,6 +201,7 @@ def __getitem__(self, key):
196201
self.done = True
197202
if isinstance(next_value, Transient):
198203
self.active_child = next_value
204+
self.active_child_key = key
199205
return next_value
200206
self.done = self.data.fast_forward(",")
201207
raise KeyError(key)

tests/test_json_stream.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ def test_as_object_that_is_partially_read_raises(complex_dict):
481481
dict_1.as_object()
482482

483483

484-
def test_as_object_grabbing_multiple_subscriptable_levels_twice_raises(complex_dict):
485-
"""Test loading a complex dict and grabbing multiple subscriptable levels twice raises."""
484+
def test_as_object_grabbing_multiple_subscriptable_levels_twice(complex_dict):
485+
"""Test loading a complex dict and grabbing multiple subscriptable levels twice."""
486486

487487
assert json.loads(complex_dict)
488488

@@ -491,5 +491,23 @@ def test_as_object_grabbing_multiple_subscriptable_levels_twice_raises(complex_d
491491
list_1 = stream["list_1"]
492492
dict_1 = next(list_1)
493493
assert dict_1["sub_dict"]["sub_dict_id"] == 1.1
494+
assert dict_1["sub_dict"]["sub_dict_name"] == "one point one"
495+
496+
497+
def test_as_object_grabbing_multiple_subscriptable_levels_again_after_passed_raises(
498+
complex_dict,
499+
):
500+
"""
501+
Test loading a complex dict and grabbing multiple subscriptable levels after passing it raises.
502+
"""
503+
504+
assert json.loads(complex_dict)
505+
506+
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
507+
508+
list_1 = stream["list_1"]
509+
dict_1 = next(list_1)
510+
assert dict_1["sub_dict"]["sub_dict_id"] == 1.1
511+
assert next(dict_1["sub_list"]) == "a"
494512
with pytest.raises(KeyError, match="sub_dict"):
495513
dict_1["sub_dict"]["sub_dict_name"]

0 commit comments

Comments
 (0)