-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import argparse | ||
from json5 import loads, dump, load | ||
from json5.loader import ModelLoader | ||
from json5.dumper import ModelDumper | ||
import sys | ||
|
||
|
||
def main(): | ||
prog = 'python -m json5.tool' | ||
description = ('Command line interface for the json5 module ' | ||
'analogous to the json.tool CLI.') | ||
parser = argparse.ArgumentParser(prog=prog, description=description) | ||
parser.add_argument('infile', nargs='?', | ||
type=argparse.FileType(encoding="utf-8"), | ||
help='a JSON file to be validated', | ||
default=sys.stdin) | ||
parser.add_argument('outfile', nargs='?', | ||
type=argparse.FileType('w', encoding="utf-8"), | ||
help='write the output of infile to outfile', | ||
default=sys.stdout) | ||
parser.add_argument('--json-lines', action='store_true', default=False, | ||
help='parse input using the JSON Lines format.') | ||
options = parser.parse_args() | ||
|
||
dump_args = { | ||
'dumper': ModelDumper() | ||
} | ||
|
||
with options.infile as infile, options.outfile as outfile: | ||
try: | ||
if options.json_lines: | ||
objs = (loads(line, loader=ModelLoader()) for line in infile) | ||
else: | ||
objs = (load(infile, loader=ModelLoader()), ) | ||
for obj in objs: | ||
dump(obj, outfile, **dump_args) | ||
outfile.write('\n') | ||
except ValueError as e: | ||
raise SystemExit(e) | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main() | ||
except BrokenPipeError as exc: | ||
sys.exit(exc.errno) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import subprocess | ||
import pytest | ||
import io | ||
@pytest.mark.parametrize('json_string', [ | ||
"""{"foo":"bar"}""", | ||
"""{"foo": "bar"}""", | ||
"""{"foo":"bar","bacon":"eggs"}""", | ||
"""{"foo": "bar", "bacon" : "eggs"}""", | ||
"""["foo","bar","baz"]""", | ||
"""[ "foo", "bar" , "baz" ]""", | ||
"""{"foo":\n "bar"\n}""", | ||
"""{"foo": {"bacon": "eggs"}}""", | ||
""" {"foo":"bar"}""", | ||
"""{"foo": "bar"} """, | ||
"""{'foo': 'bar'}""", | ||
"""{"foo": 'bar'}""", | ||
"""{"foo": "bar",}""", | ||
"""["foo","bar", "baz",]""", | ||
"""["foo", "bar", "baz", ]""", | ||
"""["foo", "bar", "baz" ,]""", | ||
"""[["foo"], ["foo","bar"], "baz"]""", | ||
"""{unquoted: "foo"}""", | ||
"""{unquoted: "foo"}""", | ||
"""["foo"]""", | ||
"""["foo" , ]""", | ||
]) | ||
def test_tool(json_string): | ||
r = subprocess.run(['python3', '-m', 'json5.tool'], text=True, input=json_string, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
assert r.stdout[:-1] == json_string |