@@ -29,7 +29,7 @@ For the development version you may visit
2929Alternatives
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
3434alternative to the built in json.loads and json.dumps using yajl. On the
3535other hand, yajl-py wraps only yajl's functionality giving the user the
@@ -58,7 +58,73 @@ that one can follow the logic.
5858Quick 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+
84150Indices and tables
85151==================
86152
0 commit comments