|
| 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