Skip to content

Commit 4cc3ece

Browse files
committed
Implemented yajl-py version of json_verify.
1 parent d53175d commit 4cc3ece

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

examples/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ the example can also be run from ../ as::
88

99
In this directory you will also find a python implementation of
1010
json_reformat, named json_reformat.py, it takes the same parameters as
11-
the C version, run it as follows for a thorough usage statement:
11+
the C version, run it as follows for a thorough usage statement::
1212

1313
python json_reformat.py -h
14+
15+
json_verify has also been implemented, to use run::
16+
17+
python json_verify.py -h

examples/json_verify.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Python implementation of the Yajl C json_verify application
3+
'''
4+
5+
import os
6+
import sys
7+
BASEPATH = os.path.dirname(os.path.realpath(__file__))
8+
sys.path = [BASEPATH, '%s/..' %BASEPATH] + sys.path
9+
from yajl import __version__ as yajl_version
10+
from yajl import *
11+
12+
import optparse
13+
14+
def main():
15+
opt_parser = optparse.OptionParser(
16+
description='validate json from stdin',
17+
version='Yajl-Py for Yajl %s' %yajl_version)
18+
opt_parser.add_option("-q",
19+
action="store_false", dest="verbose", default=True,
20+
help="quiet mode")
21+
opt_parser.add_option("-c",
22+
dest="allow_comments", action="store_true", default=False,
23+
help="allow comments")
24+
opt_parser.add_option("-u",
25+
dest="check_utf8", action='store_false', default=True,
26+
help="allow invalid utf8 inside strings")
27+
(options, args) = opt_parser.parse_args()
28+
yajl_parser = YajlParser(
29+
allow_comments=options.allow_comments,
30+
check_utf8=options.check_utf8)
31+
retval = 0
32+
try:
33+
yajl_parser.parse()
34+
except YajlError, e:
35+
retval = 1
36+
if options.verbose:
37+
sys.stderr.write(str(e))
38+
if options.verbose:
39+
print "JSON is %s" %("invalid" if retval else "valid")
40+
raise SystemExit(retval)
41+
42+
if __name__ == "__main__":
43+
main()

0 commit comments

Comments
 (0)