@@ -29,7 +29,7 @@ For the development version you may visit
29
29
Alternatives
30
30
------------
31
31
32
- Another python library that wraps yajl for python is
32
+ Another python library that wraps yajl for python is
33
33
`py-yajl <http://github.com/rtyler/py-yajl/ >`_. py-yajl creates an
34
34
alternative to the built in json.loads and json.dumps using yajl. On the
35
35
other hand, yajl-py wraps only yajl's functionality giving the user the
@@ -58,7 +58,73 @@ that one can follow the logic.
58
58
Quick Example
59
59
.............
60
60
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"]}'
62
128
63
129
Documentaion
64
130
------------
@@ -80,7 +146,7 @@ docstrings:
80
146
.. toctree ::
81
147
82
148
yajl/index
83
-
149
+
84
150
Indices and tables
85
151
==================
86
152
0 commit comments