Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_scalar_builder(self):
builder = common.ObjectBuilder()
for event, value in basic_parse(BytesIO(SCALAR_JSON)):
builder.event(event, value)
self.assertEqual(builder.value, 0)
def test_object_builder(self):
builder = common.ObjectBuilder()
for event, value in basic_parse(BytesIO(JSON)):
builder.event(event, value)
self.assertEqual(builder.value, {
'docs': [
{
'string': 'строка - тест',
'null': None,
'boolean': False,
'true': True,
'integer': 0,
'double': Decimal('0.5'),
'exponent': 100,
'long': 10000000000,
},
{
'meta': [[1], {}],
def _parse_response(self):
"""Looks for `result.item` (array), `result` (object) and `error` (object) keys and parses
the raw response content (stream of bytes)
:raise:
- ResponseError: If there's an error in the response
- MissingResult: If no result nor error was found
"""
response = self._get_response()
has_result_single = False
has_result_many = False
has_error = False
builder = ObjectBuilder()
for prefix, event, value in ijson.parse(
response.raw, buf_size=self._chunk_size
):
if (prefix, event) == ("error", "start_map"):
# Matched ServiceNow `error` object at the root
has_error = True
elif prefix == "result" and event in ["start_map", "start_array"]:
# Matched ServiceNow `result`
if event == "start_map": # Matched object
has_result_single = True
elif event == "start_array": # Matched array
has_result_many = True
if has_result_many:
# Build the result
def items(prefixed_events, prefix):
'''
An iterator returning native Python objects constructed from the events
under a given prefix.
'''
prefixed_events = iter(prefixed_events)
try:
while True:
current, event, value = next(prefixed_events)
if current == prefix:
if event in ('start_map', 'start_array'):
builder = ObjectBuilder()
end_event = event.replace('start', 'end')
while (current, event) != (prefix, end_event):
builder.event(event, value)
current, event, value = next(prefixed_events)
yield builder.value
else:
yield value
except StopIteration:
pass