Skip to content
Merged
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
21 changes: 13 additions & 8 deletions json5/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ def main(argv=None, host=None):
args.quote_style = json5.QuoteStyle.ALWAYS_DOUBLE.value

obj = json5.loads(inp, strict=args.strict)
s = json5.dumps(
obj,
indent=args.indent,
quote_keys=args.quote_keys,
trailing_commas=args.trailing_commas,
quote_style=QUOTE_STYLES[args.quote_style],
)
try:
s = json5.dumps(
obj,
indent=args.indent,
quote_keys=args.quote_keys,
trailing_commas=args.trailing_commas,
quote_style=QUOTE_STYLES[args.quote_style],
allow_nan=not args.as_json,
)
except ValueError as error:
host.print(str(error), file=host.stderr)
return 1
host.print(s)
return 0

Expand Down Expand Up @@ -126,7 +131,7 @@ def _parse_args(host, argv):
action='store_const',
const=True,
default=False,
help='output as JSON (same as --quote-keys --no-trailing-commas)',
help='output as JSON (reject non-finite numbers)',
)
parser.add_argument(
'--indent',
Expand Down
9 changes: 9 additions & 0 deletions tests/tool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ def test_as_json(self):
out='"\\u0000\\u000b"\n',
)

def test_as_json_nonfinite(self):
self.check(
['--as-json', '-c', '{value: NaN}'],
returncode=1,
out='',
err='Illegal JSON5 value: nan\n',
)
self.check(['-c', 'NaN'], out='NaN\n')

def test_quote_keys(self):
self.check(
['--quote-keys', '-c', '{foo: 1}'],
Expand Down