Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
vidjil
vidjil
Commits
cb1106a3
Commit
cb1106a3
authored
Feb 24, 2015
by
Mathieu Giraud
Browse files
tools/format_json.py: argparse, more flexible output (-1, -u)
parent
b925093e
Changes
1
Hide whitespace changes
Inline
Side-by-side
tools/format_json.py
View file @
cb1106a3
...
...
@@ -2,10 +2,18 @@ import json
import
sys
from
collections
import
OrderedDict
f
=
sys
.
stdin
if
len
(
sys
.
argv
)
>
1
:
f
=
open
(
sys
.
argv
[
1
])
s
=
f
.
read
()
print
json
.
dumps
(
OrderedDict
(
json
.
loads
(
s
,
object_pairs_hook
=
OrderedDict
))).
replace
(
'
\n
'
,
' '
)
import
argparse
parser
=
argparse
.
ArgumentParser
(
description
=
'Format a .json file'
)
parser
.
add_argument
(
'--unsorted'
,
'-u'
,
action
=
'store_true'
,
help
=
'do not sort the file (%(default)s)'
)
parser
.
add_argument
(
'--one-line'
,
'-1'
,
action
=
'store_true'
,
help
=
'one line output (%(default)d)'
)
parser
.
add_argument
(
'file'
,
nargs
=
'?'
,
type
=
argparse
.
FileType
(
'r'
),
default
=
sys
.
stdin
,
help
=
'.json file'
)
args
=
parser
.
parse_args
()
json_data
=
args
.
file
.
read
()
print
json
.
dumps
(
json
.
loads
(
json_data
,
object_pairs_hook
=
OrderedDict
),
sort_keys
=
not
args
.
unsorted
,
indent
=
None
if
args
.
one_line
else
2
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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