1'''
2Wrapper for YAJL C library version 2.x.
3'''
4
5from ctypes import byref
6
7from ijson import common, utils
8from ijson.backends import _yajl2_ctypes_common
9
10
11yajl = _yajl2_ctypes_common.get_yajl(2)
12
13# constants defined in yajl_parse.h
14YAJL_ALLOW_COMMENTS = 1
15YAJL_MULTIPLE_VALUES = 8
16
17
18@utils.coroutine
19def basic_parse_basecoro(target, allow_comments=False, multiple_values=False,
20 use_float=False):
21 '''
22 Iterator yielding unprefixed events.
23
24 Parameters:
25
26 - f: a readable file-like object with JSON input
27 - allow_comments: tells parser to allow comments in JSON input
28 - buf_size: a size of an input buffer
29 - multiple_values: allows the parser to parse multiple JSON objects
30 '''
31 callbacks = _yajl2_ctypes_common.make_callbaks(target.send, use_float, 2)
32 handle = yajl.yajl_alloc(byref(callbacks), None, None)
33 if allow_comments:
34 yajl.yajl_config(handle, YAJL_ALLOW_COMMENTS, 1)
35 if multiple_values:
36 yajl.yajl_config(handle, YAJL_MULTIPLE_VALUES, 1)
37 try:
38 while True:
39 try:
40 buffer = (yield)
41 except GeneratorExit:
42 buffer = b''
43 if buffer:
44 result = yajl.yajl_parse(handle, buffer, len(buffer))
45 else:
46 result = yajl.yajl_complete_parse(handle)
47 if result != _yajl2_ctypes_common.YAJL_OK:
48 error = _yajl2_ctypes_common.yajl_get_error(yajl, handle, buffer)
49 exception = common.IncompleteJSONError if result == _yajl2_ctypes_common.YAJL_INSUFFICIENT_DATA else common.JSONError
50 raise exception(error)
51 if not buffer:
52 break
53 finally:
54 yajl.yajl_free(handle)
55
56
57common.enrich_backend(globals())