Skip to content

Commit a1a29d9

Browse files
committed
Adding content_handler callbacks from parsing.
Details: Ading callbacks that allow for greater flexibility when stream parsing. An example of where this is usefull is using yajl_gen. json_reformat.py to follow ... which uses these new callbacks.
1 parent b7cb4c2 commit a1a29d9

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

yajl/yajl_parse.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ def yajl_start_array(self, ctx):
113113
@abstractmethod
114114
def yajl_end_array(self, ctx):
115115
pass
116-
116+
def parse_start(self):
117+
''' Called before each stream is parsed '''
118+
def parse_buf(self):
119+
''' Called when a complete buffer has been parsed from the stream '''
120+
def parse_complete(self):
121+
''' Called when the parsing of the stream has finished '''
117122

118123
class YajlParser(object):
119124
'''
@@ -200,6 +205,7 @@ def parse(self, f=sys.stdin, ctx=None):
200205
returns 0 should set internal variables to denote
201206
why they cancelled the parsing.
202207
'''
208+
self.content_handler.parse_start()
203209
hand = yajl.yajl_alloc( byref(self.callbacks), byref(self.cfg), None, ctx)
204210
try:
205211
while 1:
@@ -208,6 +214,7 @@ def parse(self, f=sys.stdin, ctx=None):
208214
stat = yajl.yajl_parse_complete(hand)
209215
else:
210216
stat = yajl.yajl_parse(hand, fileData, len(fileData))
217+
self.content_handler.parse_buf()
211218
if stat not in (yajl_status_ok.value,
212219
yajl_status_insufficient_data.value):
213220
if stat == yajl_status_client_canceled.value:
@@ -223,6 +230,7 @@ def parse(self, f=sys.stdin, ctx=None):
223230
hand, 1, fileData, len(fileData))
224231
raise YajlError(error)
225232
if not fileData:
233+
self.content_handler.parse_complete()
226234
break
227235
finally:
228236
yajl.yajl_free(hand)

0 commit comments

Comments
 (0)