Skip to content

Commit dae845a

Browse files
committed
Create YajlGen class for more pythonic yajl_gen.
Details: YajlGen allows for using python data-types without worying about the C details of yajl. All the string lengths are calculated within the YajlGen bowels such that the user of YajlGen finds its use pythonic.
1 parent 5489320 commit dae845a

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

examples/json_reformat.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,33 @@ class ReformatContentHandler(YajlContentHandler):
1717
'''
1818
def __init__(self, beautify=True, indent=" "):
1919
self.out = sys.stdout
20-
self.conf = yajl_gen_config(beautify, indent)
20+
self.beautify = beautify
21+
self.indent = indent
2122
def parse_start(self):
22-
self.g = yajl.yajl_gen_alloc(byref(self.conf), None)
23+
self.g = YajlGen(beautify=self.beautify, indent=self.indent)
2324
def parse_buf(self):
24-
l = c_uint()
25-
buf = POINTER(c_ubyte)()
26-
yajl.yajl_gen_get_buf(self.g, byref(buf), byref(l))
27-
self.out.write(string_at(buf, l.value))
28-
yajl.yajl_gen_clear(self.g)
25+
self.out.write(self.g.yajl_gen_get_buf())
2926
def parse_complete(self):
30-
yajl.yajl_gen_free(self.g)
27+
# not necessary, gc will do this @ python shutdown
28+
del self.g
3129
def yajl_null(self, ctx):
32-
yajl.yajl_gen_null(self.g)
30+
self.g.yajl_gen_null()
3331
def yajl_boolean(self, ctx, boolVal):
34-
yajl.yajl_gen_bool(self.g, boolVal)
32+
self.g.yajl_gen_bool(boolVal)
3533
def yajl_number(self, ctx, stringNum):
36-
yajl.yajl_gen_number(self.g, c_char_p(stringNum), len(stringNum))
34+
self.g.yajl_gen_number(stringNum)
3735
def yajl_string(self, ctx, stringVal):
38-
yajl.yajl_gen_string(self.g, c_char_p(stringVal), len(stringVal))
36+
self.g.yajl_gen_string(stringVal)
3937
def yajl_start_map(self, ctx):
40-
yajl.yajl_gen_map_open(self.g)
38+
self.g.yajl_gen_map_open()
4139
def yajl_map_key(self, ctx, stringVal):
42-
yajl.yajl_gen_string(self.g, c_char_p(stringVal), len(stringVal))
40+
self.g.yajl_gen_string(stringVal)
4341
def yajl_end_map(self, ctx):
44-
yajl.yajl_gen_map_close(self.g)
42+
self.g.yajl_gen_map_close()
4543
def yajl_start_array(self, ctx):
46-
yajl.yajl_gen_array_open(self.g)
44+
self.g.yajl_gen_array_open()
4745
def yajl_end_array(self, ctx):
48-
yajl.yajl_gen_array_close(self.g)
46+
self.g.yajl_gen_array_close()
4947

5048

5149
def main():

yajl/yajl_gen.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,45 @@ class yajl_gen_config(Structure):
1919
yajl_gen_invalid_number,
2020
yajl_gen_no_buf,
2121
) = map(c_int, range(7))
22+
23+
class YajlGen(object):
24+
''' Yajl Generator - json printing using yajl_gen '''
25+
def __init__(self, beautify=True, indent=" "):
26+
conf = yajl_gen_config(beautify, indent)
27+
self.g = yajl.yajl_gen_alloc(byref(conf), None)
28+
def __del__(self):
29+
yajl.yajl_gen_free(self.g)
30+
def yajl_gen_get_buf(self):
31+
l = c_uint()
32+
buf = POINTER(c_ubyte)()
33+
yajl.yajl_gen_get_buf(self.g, byref(buf), byref(l))
34+
try:
35+
return string_at(buf, l.value)
36+
finally:
37+
yajl.yajl_gen_clear(self.g)
38+
def yajl_gen_null(self):
39+
yajl.yajl_gen_null(self.g)
40+
def yajl_gen_bool(self, boolVal):
41+
yajl.yajl_gen_bool(self.g, boolVal)
42+
def yajl_gen_integer(self, number):
43+
yajl.yajl_gen_integer(self.g, number)
44+
def yajl_gen_double(self, number):
45+
yajl.yajl_gen_double(self.g, number)
46+
def yajl_gen_number(self, stringNum):
47+
'''
48+
parameter `stringNum` must be a string,
49+
otherwise use yajl_gen_double or yajl_gen_integer
50+
'''
51+
yajl.yajl_gen_number(self.g, c_char_p(stringNum), len(stringNum))
52+
def yajl_gen_string(self, stringVal):
53+
yajl.yajl_gen_string(self.g, c_char_p(stringVal), len(stringVal))
54+
def yajl_gen_map_open(self):
55+
yajl.yajl_gen_map_open(self.g)
56+
def yajl_gen_string(self, stringVal):
57+
yajl.yajl_gen_string(self.g, c_char_p(stringVal), len(stringVal))
58+
def yajl_gen_map_close(self):
59+
yajl.yajl_gen_map_close(self.g)
60+
def yajl_gen_array_open(self):
61+
yajl.yajl_gen_array_open(self.g)
62+
def yajl_gen_array_close(self):
63+
yajl.yajl_gen_array_close(self.g)

0 commit comments

Comments
 (0)