Skip to content

bpo-29636: json.tool: Add document for indentation options. #17482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 7, 2019
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
6 changes: 6 additions & 0 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ Command line options

.. versionadded:: 3.8

.. cmdoption:: --indent, --tab, --no-indent, --compact

Mutually exclusive options for whitespace control

.. versionadded:: 3.9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wimglenn does this documentation look okay? I combined multiple arguments into a single line (not sure if that violates convention or is okay)

.. cmdoption:: -h, --help

Show the help message.
Expand Down
3 changes: 2 additions & 1 deletion Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def main():
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
help='disable escaping of non-ASCII characters')
parser.add_argument('--json-lines', action='store_true', default=False,
help='parse input using the jsonlines format')
help='parse input using the JSON Lines format. '
'Use with --no-indent or --compact to produce valid JSON Lines output.')
group = parser.add_mutually_exclusive_group()
group.add_argument('--indent', default=4, type=int,
help='separate items with newlines and use this number '
Expand Down
60 changes: 27 additions & 33 deletions Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import textwrap
import unittest
from subprocess import Popen, PIPE
import subprocess
from test import support
from test.support.script_helper import assert_python_ok

Expand Down Expand Up @@ -84,10 +84,9 @@ class TestTool(unittest.TestCase):

def test_stdin_stdout(self):
args = sys.executable, '-m', 'json.tool'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
out, err = proc.communicate(self.data.encode())
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.expect)
self.assertEqual(process.stderr, '')

def _create_infile(self, data=None):
infile = support.TESTFN
Expand Down Expand Up @@ -131,10 +130,9 @@ def test_infile_outfile(self):

def test_jsonlines(self):
args = sys.executable, '-m', 'json.tool', '--json-lines'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
out, err = proc.communicate(self.jsonlines_raw.encode())
self.assertEqual(out.splitlines(), self.jsonlines_expect.encode().splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.jsonlines_expect)
self.assertEqual(process.stderr, '')

def test_help_flag(self):
rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
Expand All @@ -151,45 +149,41 @@ def test_sort_keys_flag(self):
self.assertEqual(err, b'')

def test_indent(self):
json_stdin = b'[1, 2]'
input_ = '[1, 2]'
expect = textwrap.dedent('''\
[
1,
2
]
''').encode()
''')
args = sys.executable, '-m', 'json.tool', '--indent', '2'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
json_stdout, err = proc.communicate(json_stdin)
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')

def test_no_indent(self):
json_stdin = b'[1,\n2]'
expect = b'[1, 2]'
input_ = '[1,\n2]'
expect = '[1, 2]\n'
args = sys.executable, '-m', 'json.tool', '--no-indent'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
json_stdout, err = proc.communicate(json_stdin)
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')

def test_tab(self):
json_stdin = b'[1, 2]'
expect = b'[\n\t1,\n\t2\n]\n'
input_ = '[1, 2]'
expect = '[\n\t1,\n\t2\n]\n'
args = sys.executable, '-m', 'json.tool', '--tab'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
json_stdout, err = proc.communicate(json_stdin)
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')

def test_compact(self):
json_stdin = b'[ 1 ,\n 2]'
expect = b'[1,2]'
input_ = '[ 1 ,\n 2]'
expect = '[1,2]\n'
args = sys.executable, '-m', 'json.tool', '--compact'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
json_stdout, err = proc.communicate(json_stdin)
self.assertEqual(expect.splitlines(), json_stdout.splitlines())
self.assertEqual(err, b'')
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')

def test_no_ensure_ascii_flag(self):
infile = self._create_infile('{"key":"💩"}')
Expand Down