Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ comment that can span lines /*
{"foo": "bar", "bacon": "eggs"}
```

You can also use `json5.tool` for validating json.

```
python3 -m json5.tool < myfile.json5
```
As of right now, there is no formatting on the output; original whitespace and comments is maintained.
In the future, pretty-printing, stripping comments, and other options may be added.

See also the [full documentation](https://json-five.readthedocs.io/en/latest/)

## Key features
Expand Down
2 changes: 1 addition & 1 deletion json5/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def process_leading_wsc(self, node):

@singledispatchmethod
def dump(self, node):
raise NotImplementedError('foo')
raise NotImplementedError(f'Cannot dump node of type {type(node)}')

to_json = dump.register

Expand Down
2 changes: 1 addition & 1 deletion json5/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def load(f, **kwargs):
:return:
"""
text = f.read()
return loads(text)
return loads(text, **kwargs)

def loads(s, *args, loader=None, **kwargs):
"""
Expand Down
46 changes: 46 additions & 0 deletions json5/tool.py
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='json-five',
version='0.6.5',
version='0.7.5',
packages=['json5'],
url='https://github.com/spyoungtech/json-five',
license='Apache',
Expand Down
35 changes: 35 additions & 0 deletions tests/test_json_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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"],
universal_newlines=True,
input=json_string,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
assert r.stdout[:-1] == json_string