Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ijson_top_level_items(file, local_streaming_backend):
ijson = get_ijson(local_streaming_backend)
parser = ijson.parse(file)
prefixed_events = iter(parser)
wanted = None
try:
while True:
current, event, value = next(prefixed_events)
if current != '':
wanted = current
if event in ('start_map', 'start_array'):
builder = ijson_mod.ObjectBuilder()
end_event = event.replace('start', 'end')
while (current, event) != (wanted, end_event):
builder.event(event, value)
current, event, value = next(prefixed_events)
yield current, builder.value
except StopIteration:
pass
def streamJsonArrayItems(f):
# read items from a json array -- without loading the entire file into memory
level = 0
currentObject = ijson.ObjectBuilder()
parsed = ijson.parse(f)
# eat the initial start_array event
assertEqual('start_array', next(parsed)[1])
# construct objects. use level in order to support objects within objects
for _, event, value in parsed:
currentObject.event(event, value)
if event == 'start_map':
level += 1
elif event == 'end_map':
level -= 1
if level == 0:
yield currentObject.value
currentObject = ijson.ObjectBuilder()
array_stack = 0
top_level_object = False
object_stack = 0
parser = ijson.parse(open(filepath, 'r'))
for prefix, event, value in parser:
if event == 'start_array':
if not top_level_array:
top_level_array = True
continue
else:
array_stack += 1
if event == 'start_map':
if not top_level_object:
top_level_object = True
builder = ijson.ObjectBuilder()
else:
object_stack += 1
if event == 'end_map':
if not top_level_object:
raise Exception('end_map without a top level object')
else:
if object_stack == 0:
top_level_object = False
yield builder.value
else:
object_stack -= 1
if event == 'end_array':
if not top_level_array:
raise Exception('end_array without a top level array')
else:
if array_stack == 0:
currentObject = ijson.ObjectBuilder()
parsed = ijson.parse(f)
# eat the initial start_array event
assertEqual('start_array', next(parsed)[1])
# construct objects. use level in order to support objects within objects
for _, event, value in parsed:
currentObject.event(event, value)
if event == 'start_map':
level += 1
elif event == 'end_map':
level -= 1
if level == 0:
yield currentObject.value
currentObject = ijson.ObjectBuilder()