Skip to content

Commit f97de48

Browse files
committed
Updating json_reformat to model yajl's json_reformat.
1 parent 9681451 commit f97de48

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ tmp/
1515
dist/
1616
build/
1717
*.egg-info/
18+
19+
# developer specifics
20+
.virtualenv/
21+
.vagrant/
22+
Vagrantfile

examples/json_reformat.py

+71-18
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,115 @@
55
import os
66
import sys
77
BASEPATH = os.path.dirname(os.path.realpath(__file__))
8-
sys.path = [BASEPATH, '%s/..' %BASEPATH] + sys.path
8+
sys.path = [BASEPATH, '%s/..' % BASEPATH] + sys.path
99
from yajl import __version__ as yajl_version
10-
from yajl import *
10+
from yajl import (
11+
__version__ as yajl_version,
12+
YajlContentHandler, YajlGen, YajlParser,
13+
YajlGenException,
14+
)
1115

1216
import optparse
1317

18+
1419
class ReformatContentHandler(YajlContentHandler):
1520
'''
1621
Content handler to reformat a json file using yajl_gen
1722
'''
18-
def __init__(self, beautify=True, indent_string=" "):
23+
def __init__(self, beautify=True, indent_string=" ", stream=False):
1924
self.out = sys.stdout
2025
self.beautify = beautify
2126
self.indent_string = indent_string
27+
self.stream = stream
2228
def parse_start(self):
23-
self.g = YajlGen(beautify=self.beautify, indent_string=self.indent_string)
29+
self.g = YajlGen(
30+
beautify=self.beautify,
31+
indent_string=self.indent_string,
32+
)
2433
def parse_buf(self):
2534
self.out.write(self.g.yajl_gen_get_buf())
2635
def parse_complete(self):
27-
# not necessary, gc will do this @ python shutdown
28-
del self.g
36+
if not stream:
37+
# not necessary, gc will do this @ python shutdown
38+
del self.g
39+
def check_and_return(self, func, *args, **kwargs):
40+
try:
41+
func(*args, **kwargs)
42+
except YajlGenException as e:
43+
if self.stream and e.value == 'yajl_gen_generation_complete':
44+
self.g.yajl_gen_reset('\n')
45+
func(*args, **kwargs)
46+
else:
47+
raise
2948
def yajl_null(self, ctx):
30-
self.g.yajl_gen_null()
49+
self.check_and_return(
50+
self.g.yajl_gen_null,
51+
)
3152
def yajl_boolean(self, ctx, boolVal):
32-
self.g.yajl_gen_bool(boolVal)
53+
self.check_and_return(
54+
self.g.yajl_gen_bool,
55+
boolVal
56+
)
3357
def yajl_number(self, ctx, stringNum):
34-
self.g.yajl_gen_number(stringNum)
58+
self.check_and_return(
59+
self.g.yajl_gen_number,
60+
stringNum
61+
)
3562
def yajl_string(self, ctx, stringVal):
36-
self.g.yajl_gen_string(stringVal)
63+
self.check_and_return(
64+
self.g.yajl_gen_string,
65+
stringVal
66+
)
3767
def yajl_start_map(self, ctx):
38-
self.g.yajl_gen_map_open()
68+
self.check_and_return(
69+
self.g.yajl_gen_map_open,
70+
)
3971
def yajl_map_key(self, ctx, stringVal):
40-
self.g.yajl_gen_string(stringVal)
72+
self.check_and_return(
73+
self.g.yajl_gen_string,
74+
stringVal
75+
)
4176
def yajl_end_map(self, ctx):
42-
self.g.yajl_gen_map_close()
77+
self.check_and_return(
78+
self.g.yajl_gen_map_close,
79+
)
4380
def yajl_start_array(self, ctx):
44-
self.g.yajl_gen_array_open()
81+
self.check_and_return(
82+
self.g.yajl_gen_array_open,
83+
)
4584
def yajl_end_array(self, ctx):
46-
self.g.yajl_gen_array_close()
85+
self.check_and_return(
86+
self.g.yajl_gen_array_close,
87+
)
4788

4889

4990
def main():
5091
opt_parser = optparse.OptionParser(
5192
description='reformat json from stdin',
52-
version='Yajl-Py for Yajl %s' %yajl_version)
93+
version='Yajl-Py for Yajl %s' % yajl_version)
5394
opt_parser.add_option("-m",
5495
dest="beautify", action="store_false", default=True,
5596
help="minimize json rather than beautify (default)")
5697
opt_parser.add_option("-u",
5798
dest="dont_validate_strings", action='store_true', default=False,
5899
help="allow invalid UTF8 inside strings during parsing")
100+
opt_parser.add_option("-e",
101+
dest="escape_solidus", action='store_true', default=False,
102+
help="escape any forward slashes (for embedding in HTML)")
103+
opt_parser.add_option("-s",
104+
dest="stream", action='store_true', default=False,
105+
help="reformat a stream of multiple json entites")
59106
(options, args) = opt_parser.parse_args()
60-
ch = ReformatContentHandler(beautify=options.beautify)
107+
# initialize the content handler (creates a Yajl Gen)
108+
ch = ReformatContentHandler(
109+
beautify=options.beautify,
110+
stream=options.stream,
111+
)
112+
# initialize the parser
61113
yajl_parser = YajlParser(ch)
62-
yajl_parser.allow_comments = True # let's allow comments by default
114+
yajl_parser.allow_comments = True # let's allow comments by default
63115
yajl_parser.dont_validate_strings = options.dont_validate_strings
116+
yajl_parser.allow_multiple_values = options.stream
64117
yajl_parser.parse()
65118

66119
if __name__ == "__main__":

examples/yajl_py_example.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
BASEPATH = os.path.dirname(os.path.realpath(__file__))
44
sys.path = [BASEPATH, '%s/..' %BASEPATH] + sys.path
5-
from yajl import *
5+
from yajl import YajlContentHandler, YajlParser
66

77
# Sample callbacks, which output some debug info
88
# these are examples to show off the yajl parser
@@ -37,6 +37,7 @@ def yajl_end_array(self, ctx):
3737

3838
def main(args):
3939
parser = YajlParser(ContentHandler())
40+
parser.allow_multiple_values = True
4041
if args:
4142
for fn in args:
4243
f = open(fn)

0 commit comments

Comments
 (0)