1'''
2Wrapper for YAJL C library version 1.x.
3'''
4
5import ctypes
6from ctypes import Structure, c_uint, byref
7
8from ijson import common, utils
9from ijson.backends import _yajl2_ctypes_common
10
11
12yajl = _yajl2_ctypes_common.get_yajl(1)
13
14class Config(Structure):
15 _fields_ = [
16 ("allowComments", c_uint),
17 ("checkUTF8", c_uint)
18 ]
19
20
21@utils.coroutine
22def basic_parse_basecoro(target, allow_comments=False, multiple_values=False,
23 use_float=False):
24 '''
25 Iterator yielding unprefixed events.
26
27 Parameters:
28
29 - f: a readable file-like object with JSON input
30 - allow_comments: tells parser to allow comments in JSON input
31 - check_utf8: if True, parser will cause an error if input is invalid utf-8
32 - buf_size: a size of an input buffer
33 '''
34 if multiple_values:
35 raise ValueError("yajl backend doesn't support multiple_values")
36 callbacks, _keepalive = _yajl2_ctypes_common.make_callbaks(target.send, use_float, 1)
37 config = Config(allow_comments, True)
38 handle = yajl.yajl_alloc(byref(callbacks), byref(config), None, None)
39 try:
40 while True:
41 try:
42 buffer = (yield)
43 except GeneratorExit:
44 buffer = b''
45 if buffer:
46 result = yajl.yajl_parse(handle, buffer, len(buffer))
47 else:
48 result = yajl.yajl_parse_complete(handle)
49 if result == _yajl2_ctypes_common.YAJL_ERROR:
50 error = _yajl2_ctypes_common.yajl_get_error(yajl, handle, buffer)
51 raise common.JSONError(error)
52 elif not buffer:
53 if result == _yajl2_ctypes_common.YAJL_INSUFFICIENT_DATA:
54 raise common.IncompleteJSONError('Incomplete JSON data')
55 break
56 finally:
57 yajl.yajl_free(handle)
58
59
60common.enrich_backend(
61 globals(),
62 multiple_values=False,
63 invalid_leading_zeros_detection=False,
64 incomplete_json_tokens_detection=False,
65 int64=ctypes.sizeof(ctypes.c_long) == 0,
66)