Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
940e2074
Kaydet (Commit)
940e2074
authored
Mar 22, 2014
tarafından
Benjamin Peterson
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
improve the command-line interface of json.tool (closes #21000)
A patch from Berker Peksag.
üst
a191b91a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
12 deletions
+76
-12
json.rst
Doc/library/json.rst
+51
-0
tool.py
Lib/json/tool.py
+15
-12
test_tool.py
Lib/test/test_json/test_tool.py
+8
-0
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/json.rst
Dosyayı görüntüle @
940e2074
...
...
@@ -104,6 +104,8 @@ Using json.tool from the shell to validate and pretty-print::
$ echo '{1.2:3.4}' | python -mjson.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
See :ref:`json-commandline` for detailed documentation.
.. highlight:: python3
.. note::
...
...
@@ -563,3 +565,52 @@ the last name-value pair for a given name::
{'x': 3}
The *object_pairs_hook* parameter can be used to alter this behavior.
.. highlight:: bash
.. _json-commandline:
Command Line Interface
----------------------
The :mod:`json.tool` module provides a simple command line interface to validate
and pretty-print JSON objects.
If the optional :option:`infile` and :option:`outfile` arguments are not
specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively::
$ echo '{"json": "obj"}' | python -m json.tool
{
"json": "obj"
}
$ echo '{1.2:3.4}' | python -m json.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Command line options
^^^^^^^^^^^^^^^^^^^^
.. cmdoption:: [<infile>]
The JSON file to be validated or pretty-printed::
$ python -m json.tool mp_films.json
[
{
"title": "And Now for Something Completely Different",
"year": 1971
},
{
"title": "Monty Python and the Holy Grail",
"year": 1975
}
]
.. cmdoption:: [<outfile>]
Write the output of the *infile* to the given *outfile*. Otherwise, write it
to :attr:`sys.stdout`.
.. cmdoption:: -h, --help
Show the help message.
Lib/json/tool.py
Dosyayı görüntüle @
940e2074
...
...
@@ -10,21 +10,24 @@ Usage::
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
"""
import
sys
import
argparse
import
json
import
sys
def
main
():
if
len
(
sys
.
argv
)
==
1
:
infile
=
sys
.
stdin
outfile
=
sys
.
stdout
elif
len
(
sys
.
argv
)
==
2
:
infile
=
open
(
sys
.
argv
[
1
],
'r'
)
outfile
=
sys
.
stdout
elif
len
(
sys
.
argv
)
==
3
:
infile
=
open
(
sys
.
argv
[
1
],
'r'
)
outfile
=
open
(
sys
.
argv
[
2
],
'w'
)
else
:
raise
SystemExit
(
sys
.
argv
[
0
]
+
" [infile [outfile]]"
)
prog
=
'python -m json.tool'
description
=
(
'A simple command line interface for json module '
'to validate and pretty-print JSON objects.'
)
parser
=
argparse
.
ArgumentParser
(
prog
=
prog
,
description
=
description
)
parser
.
add_argument
(
'infile'
,
nargs
=
'?'
,
type
=
argparse
.
FileType
(),
help
=
'a JSON file to be validated or pretty-printed'
)
parser
.
add_argument
(
'outfile'
,
nargs
=
'?'
,
type
=
argparse
.
FileType
(
'w'
),
help
=
'write the output of infile to outfile'
)
options
=
parser
.
parse_args
()
infile
=
options
.
infile
or
sys
.
stdin
outfile
=
options
.
outfile
or
sys
.
stdout
with
infile
:
try
:
obj
=
json
.
load
(
infile
)
...
...
Lib/test/test_json/test_tool.py
Dosyayı görüntüle @
940e2074
...
...
@@ -55,6 +55,7 @@ class TestTool(unittest.TestCase):
def
test_infile_stdout
(
self
):
infile
=
self
.
_create_infile
()
rc
,
out
,
err
=
assert_python_ok
(
'-m'
,
'json.tool'
,
infile
)
self
.
assertEqual
(
rc
,
0
)
self
.
assertEqual
(
out
.
splitlines
(),
self
.
expect
.
encode
()
.
splitlines
())
self
.
assertEqual
(
err
,
b
''
)
...
...
@@ -65,5 +66,12 @@ class TestTool(unittest.TestCase):
self
.
addCleanup
(
os
.
remove
,
outfile
)
with
open
(
outfile
,
"r"
)
as
fp
:
self
.
assertEqual
(
fp
.
read
(),
self
.
expect
)
self
.
assertEqual
(
rc
,
0
)
self
.
assertEqual
(
out
,
b
''
)
self
.
assertEqual
(
err
,
b
''
)
def
test_help_flag
(
self
):
rc
,
out
,
err
=
assert_python_ok
(
'-m'
,
'json.tool'
,
'-h'
)
self
.
assertEqual
(
rc
,
0
)
self
.
assertTrue
(
out
.
startswith
(
b
'usage: '
))
self
.
assertEqual
(
err
,
b
''
)
Misc/NEWS
Dosyayı görüntüle @
940e2074
...
...
@@ -23,6 +23,8 @@ Core and Builtins
Library
-------
- Issue #21000: Improve the command-line interface of json.tool.
- Issue #20995: Enhance default ciphers used by the ssl module to enable
better security an prioritize perfect forward secrecy.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment