Пример #1
0
def main():
    opt_parser = optparse.OptionParser(
        description='reformat json from stdin',
        version='Yajl-Py for Yajl %s' % yajl_version)
    opt_parser.add_option("-m",
        dest="beautify", action="store_false", default=True,
        help="minimize json rather than beautify (default)")
    opt_parser.add_option("-u",
        dest="dont_validate_strings", action='store_true', default=False,
        help="allow invalid UTF8 inside strings during parsing")
    opt_parser.add_option("-e",
        dest="escape_solidus", action='store_true', default=False,
        help="escape any forward slashes (for embedding in HTML)")
    opt_parser.add_option("-s",
        dest="stream", action='store_true', default=False,
        help="reformat a stream of multiple json entites")
    (options, args) = opt_parser.parse_args()
    # initialize the content handler (creates a Yajl Gen)
    ch = ReformatContentHandler(
        beautify=options.beautify,
        stream=options.stream,
    )
    # initialize the parser
    yajl_parser = YajlParser(ch)
    yajl_parser.allow_comments = True  # let's allow comments by default
    yajl_parser.allow_multiple_values = True
    yajl_parser.dont_validate_strings = options.dont_validate_strings
    yajl_parser.allow_multiple_values = options.stream
    yajl_parser.parse()
Пример #2
0
def main():
    opt_parser = optparse.OptionParser(
        description='validate json from stdin',
        version='Yajl-Py for Yajl %s' %yajl_version)
    opt_parser.add_option("-q",
        action="store_false", dest="verbose", default=True,
        help="quiet mode")
    opt_parser.add_option("-c",
        dest="allow_comments", action="store_true", default=False,
        help="allow comments")
    opt_parser.add_option("-u",
        dest="dont_validate_strings", action='store_true', default=False,
        help="allow invalid utf8 inside strings")
    (options, args) = opt_parser.parse_args()
    yajl_parser = YajlParser()
    yajl_parser.allow_comments = options.allow_comments
    yajl_parser.dont_validate_strings = options.dont_validate_strings
    retval = 0
    try:
        yajl_parser.parse()
    except YajlError as e:
        retval = 1
        if options.verbose:
            sys.stderr.write(e.value.decode('utf-8'))
    if options.verbose:
        print("JSON is %s" %("invalid" if retval else "valid"))
    raise SystemExit(retval)
Пример #3
0
def main():
    opt_parser = optparse.OptionParser(description='reformat json from stdin',
                                       version='Yajl-Py for Yajl %s' %
                                       yajl_version)
    opt_parser.add_option("-m",
                          dest="beautify",
                          action="store_false",
                          default=True,
                          help="minimize json rather than beautify (default)")
    opt_parser.add_option(
        "-u",
        dest="dont_validate_strings",
        action='store_true',
        default=False,
        help="allow invalid UTF8 inside strings during parsing")
    opt_parser.add_option(
        "-e",
        dest="escape_solidus",
        action='store_true',
        default=False,
        help="escape any forward slashes (for embedding in HTML)")
    opt_parser.add_option("-s",
                          dest="stream",
                          action='store_true',
                          default=False,
                          help="reformat a stream of multiple json entites")
    (options, args) = opt_parser.parse_args()
    # initialize the content handler (creates a Yajl Gen)
    ch = ReformatContentHandler(
        beautify=options.beautify,
        stream=options.stream,
    )
    # initialize the parser
    yajl_parser = YajlParser(ch)
    yajl_parser.allow_comments = True  # let's allow comments by default
    yajl_parser.allow_multiple_values = True
    yajl_parser.dont_validate_strings = options.dont_validate_strings
    yajl_parser.allow_multiple_values = options.stream
    yajl_parser.parse()