Skip to content

Commit 59a4085

Browse files
committed
Updated docs.
Details: Fixed up yajl_parse docs and added quick examples.
1 parent df8ede1 commit 59a4085

File tree

2 files changed

+90
-17
lines changed

2 files changed

+90
-17
lines changed

doc/source/index.rst

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For the development version you may visit
2929
Alternatives
3030
------------
3131

32-
Another python library that wraps yajl for python is
32+
Another python library that wraps yajl for python is
3333
`py-yajl <http://github.com/rtyler/py-yajl/>`_. py-yajl creates an
3434
alternative to the built in json.loads and json.dumps using yajl. On the
3535
other hand, yajl-py wraps only yajl's functionality giving the user the
@@ -58,7 +58,73 @@ that one can follow the logic.
5858
Quick Example
5959
.............
6060

61-
.. write some quick example here
61+
Parsing
62+
+++++++
63+
.. code-block:: python
64+
65+
import sys
66+
from yajl import *
67+
68+
# Sample callbacks, which output some debug info
69+
# these are examples to show off the yajl parser
70+
class ContentHandler(YajlContentHandler):
71+
def __init__(self):
72+
self.out = sys.stdout
73+
def yajl_null(self, ctx):
74+
self.out.write("null\n" )
75+
def yajl_boolean(self, ctx, boolVal):
76+
self.out.write("bool: %s\n" %('true' if boolVal else 'false'))
77+
def yajl_integer(self, ctx, integerVal):
78+
self.out.write("integer: %s\n" %integerVal)
79+
def yajl_double(self, ctx, doubleVal):
80+
self.out.write("double: %s\n" %doubleVal)
81+
def yajl_number(self, ctx, stringNum):
82+
''' Since this is defined both integer and double callbacks are useless '''
83+
num = float(stringNum) if '.' in stringNum else int(stringNum)
84+
self.out.write("number: %s\n" %num)
85+
def yajl_string(self, ctx, stringVal):
86+
self.out.write("string: '%s'\n" %stringVal)
87+
def yajl_start_map(self, ctx):
88+
self.out.write("map open '{'\n")
89+
def yajl_map_key(self, ctx, stringVal):
90+
self.out.write("key: '%s'\n" %stringVal)
91+
def yajl_end_map(self, ctx):
92+
self.out.write("map close '}'\n")
93+
def yajl_start_array(self, ctx):
94+
self.out.write("array open '['\n")
95+
def yajl_end_array(self, ctx):
96+
self.out.write("array close ']'\n")
97+
98+
# Create the parser
99+
parser = YajlParser(ContentHandler())
100+
# Parse JSON from stdin
101+
parser.parse()
102+
103+
Generating
104+
++++++++++
105+
.. code-block:: python
106+
107+
from yajl import *
108+
109+
g = YajlGen(beautify=False)
110+
g.yajl_gen_map_open()
111+
g.yajl_gen_string("a")
112+
g.yajl_gen_array_open()
113+
g.yajl_gen_null()
114+
g.yajl_gen_bool(True)
115+
g.yajl_gen_integer(1)
116+
g.yajl_gen_double(2.0)
117+
g.yajl_gen_number(str(3))
118+
119+
g.yajl_gen_get_buf()
120+
# [Out]: '{"a":[null,true,1,2,3'
121+
122+
g.yajl_gen_string("b")
123+
g.yajl_gen_array_close()
124+
g.yajl_gen_map_close()
125+
126+
g.yajl_gen_get_buf()
127+
# [Out]: ',"b"]}'
62128
63129
Documentaion
64130
------------
@@ -80,7 +146,7 @@ docstrings:
80146
.. toctree::
81147

82148
yajl/index
83-
149+
84150
Indices and tables
85151
==================
86152

yajl/yajl_parse.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class YajlContentHandler(object):
7373
the :meth:`yajl_integer` & :meth:`yajl_double` callbacks will be ignored.
7474
For this reason none of these three methods are enforced by the Abstract
7575
Base Class
76+
77+
**Note** all methods must accept a param :obj:`ctx` as the first argument,
78+
this is a yajl feature that is implemented in yajl-py but not very useful
79+
in python. see :meth:`YajlParser.parse` for more info on :obj:`ctx`.
7680
'''
7781
__metaclass__ = ABCMeta
7882
@abstractmethod
@@ -121,10 +125,16 @@ class YajlParser(object):
121125
'''
122126
def __init__(self, content_handler=None, allow_comments=True, check_utf8=True, buf_siz=65536):
123127
'''
124-
`content_handler` an instance of a subclass of YajlContentHandler
125-
`allow_comments` specifies whether comments are allowed in the document
126-
`check_utf8` specifies whether utf8 charachters are allowed in the document
127-
`buf_siz` the number of bytes to process from the input stream at a time
128+
:type content_handler: :class:`YajlContentHandler`
129+
:param content_handler: content handler instance hosting the
130+
callbacks that will be called while parsing.
131+
:type allow_comments: bool
132+
:param allow_comments: if comments are allowed in the document
133+
:type check_utf8: bool
134+
:param check_utf8: if utf8 charachters are allowed in the document
135+
:type buf_siz: int
136+
:param buf_siz: number of bytes to process from the input stream
137+
at a time (minimum 1)
128138
'''
129139
# input validation
130140
if buf_siz <= 0:
@@ -194,16 +204,13 @@ def dispatch(func, *args, **kwargs):
194204
def parse(self, f=sys.stdin, ctx=None):
195205
'''Function to parse a JSON stream.
196206
197-
Parameters:
198-
199-
* `f` - file stream to read from
200-
* `ctx` - A ctypes pointer that will be passed to all
201-
callback functions as the first param
202-
203-
Raises an expception upon error or return value of 0
204-
from callback functions. A callback function that
205-
returns 0 should set internal variables to denote
206-
why they cancelled the parsing.
207+
:type f: file
208+
:param f: stream to parse JSON from
209+
:type ctx: ctypes.POINTER
210+
:param ctx: passed to all callback functions as the first param this is
211+
a feature of yajl, and not very useful in yajl-py since the context is
212+
preserved using the content_handler instance.
213+
:raises YajlError: When invalid JSON in input stream found
207214
'''
208215
if self.content_handler:
209216
self.content_handler.parse_start()

0 commit comments

Comments
 (0)