@@ -154,7 +154,7 @@ def __init__(self, stream):
154
154
self .finish_char = ""
155
155
156
156
def finish (self ):
157
- """Consume all of the characters for this list from the stream."""
157
+ """Consume all of the characters for this container from the stream."""
158
158
if not self .done :
159
159
if self .active_child :
160
160
self .active_child .finish ()
@@ -163,7 +163,8 @@ def finish(self):
163
163
self .done = True
164
164
165
165
def as_object (self ):
166
- """Consume all of the characters for this list from the stream and return as an object."""
166
+ """Consume all of the characters for this container from the stream
167
+ and return as an object."""
167
168
if self .has_read :
168
169
raise BufferError ("Object has already been partly read." )
169
170
@@ -207,10 +208,17 @@ class TransientObject(Transient):
207
208
def __init__ (self , stream ):
208
209
super ().__init__ (stream )
209
210
self .finish_char = "}"
210
- self .active_child_key = None
211
+ self .active_key = None
212
+
213
+ def finish (self ):
214
+ """Consume all of the characters for this container from the stream."""
215
+ if self .active_key and not self .active_child :
216
+ self .done = self .data .fast_forward ("," )
217
+ self .active_key = None
218
+ super ().finish ()
211
219
212
220
def __getitem__ (self , key ):
213
- if self .active_child and self .active_child_key == key :
221
+ if self .active_child and self .active_key == key :
214
222
return self .active_child
215
223
216
224
self .has_read = True
@@ -219,12 +227,16 @@ def __getitem__(self, key):
219
227
self .active_child .finish ()
220
228
self .done = self .data .fast_forward ("," )
221
229
self .active_child = None
222
- self .active_child_key = None
230
+ self .active_key = None
223
231
if self .done :
224
232
raise KeyError (key )
225
233
226
234
while not self .done :
227
- current_key = self .data .next_value (":" )
235
+ if self .active_key :
236
+ current_key = self .active_key
237
+ self .active_key = None
238
+ else :
239
+ current_key = self .data .next_value (":" )
228
240
if current_key is None :
229
241
self .done = True
230
242
break
@@ -234,43 +246,44 @@ def __getitem__(self, key):
234
246
self .done = True
235
247
if isinstance (next_value , Transient ):
236
248
self .active_child = next_value
237
- self .active_child_key = key
249
+ self .active_key = key
238
250
return next_value
239
251
self .done = self .data .fast_forward ("," )
240
252
raise KeyError (key )
241
253
242
254
def __iter__ (self ):
243
255
return self
244
256
245
- def _next_item (self ):
246
- """Return the next item as a (key, value) pair, regardless of key."""
247
- if self .active_child :
248
- self .active_child .finish ()
257
+ def _next_key (self ):
258
+ """Return the next item's key, without consuming the value."""
259
+ if self .active_key :
260
+ if self .active_child :
261
+ self .active_child .finish ()
262
+ self .active_child = None
249
263
self .done = self .data .fast_forward ("," )
250
- self .active_child = None
264
+ self .active_key = None
251
265
if self .done :
252
266
raise StopIteration ()
253
267
268
+ self .has_read = True
269
+
254
270
current_key = self .data .next_value (":" )
255
271
if current_key is None :
256
272
self .done = True
257
273
raise StopIteration ()
258
274
259
- next_value = self .data .next_value ("," )
260
- if self .data .last_char == ord ("}" ):
261
- self .done = True
262
- if isinstance (next_value , Transient ):
263
- self .active_child = next_value
264
- return (current_key , next_value )
275
+ self .active_key = current_key
276
+ return current_key
265
277
266
278
def __next__ (self ):
267
- return self ._next_item ()[ 0 ]
279
+ return self ._next_key ()
268
280
269
281
def items (self ):
270
- """Return iterator ine the dictionary’s items ((key, value) pairs)."""
282
+ """Return iterator in the dictionary’s items ((key, value) pairs)."""
271
283
try :
272
284
while not self .done :
273
- yield self ._next_item ()
285
+ key = self ._next_key ()
286
+ yield (key , self [key ])
274
287
except StopIteration :
275
288
return
276
289
0 commit comments