Skip to content

Commit fd5d8a4

Browse files
committed
Cleanup return_object
1 parent f333823 commit fd5d8a4

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

adafruit_json_stream.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@ def read(self):
4040
self.i += 1
4141
return char
4242

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

4848
closer = ord(closer)
4949
close_stack = [closer]
5050
count = 0
51+
52+
buffer = None
53+
if return_object:
54+
buffer = bytearray(32)
55+
# ] = 93, [ = 91
56+
# } = 125, { = 123
57+
buffer[0] = closer - 2
58+
5159
while close_stack:
5260
char = self.read()
5361
count += 1
@@ -114,7 +122,6 @@ def __init__(self, stream):
114122
self.done = False
115123
self.has_read = False
116124
self.finish_char = ""
117-
self.start_char = ""
118125

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

133-
buffer = bytearray(32)
134-
buffer[0] = ord(self.start_char)
135140
self.done = True
136-
return self.data.fast_forward(self.finish_char, buffer)
141+
return self.data.fast_forward(self.finish_char, return_object=True)
137142

138143

139144
class TransientList(Transient):
@@ -142,7 +147,6 @@ class TransientList(Transient):
142147
def __init__(self, stream):
143148
super().__init__(stream)
144149
self.finish_char = "]"
145-
self.start_char = "["
146150

147151
def __iter__(self):
148152
return self
@@ -173,7 +177,6 @@ class TransientObject(Transient):
173177
def __init__(self, stream):
174178
super().__init__(stream)
175179
self.finish_char = "}"
176-
self.start_char = "{"
177180
self.active_child_key = None
178181

179182
def __getitem__(self, key):

0 commit comments

Comments
 (0)