Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iter issues #7

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup return_object
  • Loading branch information
justmobilize committed Mar 3, 2025
commit fd5d8a45293c148b6fd0a9ef6d29851a5ea06ecc
17 changes: 10 additions & 7 deletions adafruit_json_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ def read(self):
self.i += 1
return char

def fast_forward(self, closer, buffer=None):
def fast_forward(self, closer, *, return_object=False):
"""Read through the stream until the character is ``closer``, ``]``
(ending a list) or ``}`` (ending an object.) Intermediate lists and
objects are skipped."""

closer = ord(closer)
close_stack = [closer]
count = 0

buffer = None
if return_object:
buffer = bytearray(32)
# ] = 93, [ = 91
# } = 125, { = 123
buffer[0] = closer - 2

while close_stack:
char = self.read()
count += 1
Expand Down Expand Up @@ -114,7 +122,6 @@ def __init__(self, stream):
self.done = False
self.has_read = False
self.finish_char = ""
self.start_char = ""

def finish(self):
"""Consume all of the characters for this list from the stream."""
Expand All @@ -130,10 +137,8 @@ def as_object(self):
if self.has_read:
raise BufferError("Object has already been partly read.")

buffer = bytearray(32)
buffer[0] = ord(self.start_char)
self.done = True
return self.data.fast_forward(self.finish_char, buffer)
return self.data.fast_forward(self.finish_char, return_object=True)


class TransientList(Transient):
Expand All @@ -142,7 +147,6 @@ class TransientList(Transient):
def __init__(self, stream):
super().__init__(stream)
self.finish_char = "]"
self.start_char = "["

def __iter__(self):
return self
Expand Down Expand Up @@ -173,7 +177,6 @@ class TransientObject(Transient):
def __init__(self, stream):
super().__init__(stream)
self.finish_char = "}"
self.start_char = "{"
self.active_child_key = None

def __getitem__(self, key):
Expand Down