Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b643ef8f
Commit
b643ef8f
authored
Oct 31, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #5729: json.dumps to support using '\t' as an indent string
parent
6ff2a7d1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
26 deletions
+38
-26
Doc/library/json.rst
Doc/library/json.rst
+6
-4
Lib/json/encoder.py
Lib/json/encoder.py
+7
-4
Lib/json/tests/test_indent.py
Lib/json/tests/test_indent.py
+22
-18
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/json.rst
View file @
b643ef8f
...
@@ -135,10 +135,12 @@ Basic Usage
...
@@ -135,10 +135,12 @@ Basic Usage
``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
If *indent* is a non-negative integer, then JSON array elements and object
If *indent* is a non-negative integer or string, then JSON array elements and
members will be pretty-printed with that indent level. An indent level of 0
object members will be pretty-printed with that indent level. An indent level
will only insert newlines. ``None`` (the default) selects the most compact
of 0 or ``""`` will only insert newlines. ``None`` (the default) selects the
representation.
most compact representation. Using an integer indent indents that many spaces
per level. If *indent* is a string (such at '\t'), that string is used to indent
each level.
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
...
...
Lib/json/encoder.py
View file @
b643ef8f
...
@@ -259,6 +259,9 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
...
@@ -259,6 +259,9 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
tuple=tuple,
tuple=tuple,
):
):
if _indent is not None and not isinstance(_indent, str):
_indent = ' ' * _indent
def _iterencode_list(lst, _current_indent_level):
def _iterencode_list(lst, _current_indent_level):
if not lst:
if not lst:
yield '[]'
yield '[]'
...
@@ -271,7 +274,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
...
@@ -271,7 +274,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
buf = '['
buf = '['
if _indent is not None:
if _indent is not None:
_current_indent_level += 1
_current_indent_level += 1
newline_indent = '
\
n
' +
(' ' * (_indent * _current_indent_level))
newline_indent = '
\
n
' +
_indent * _current_indent_level
separator = _item_separator + newline_indent
separator = _item_separator + newline_indent
buf += newline_indent
buf += newline_indent
else:
else:
...
@@ -307,7 +310,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
...
@@ -307,7 +310,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield chunk
yield chunk
if newline_indent is not None:
if newline_indent is not None:
_current_indent_level -= 1
_current_indent_level -= 1
yield '
\
n
' +
(' ' * (_indent * _current_indent_level))
yield '
\
n
' +
_indent * _current_indent_level
yield ']'
yield ']'
if markers is not None:
if markers is not None:
del markers[markerid]
del markers[markerid]
...
@@ -324,7 +327,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
...
@@ -324,7 +327,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield '{'
yield '{'
if _indent is not None:
if _indent is not None:
_current_indent_level += 1
_current_indent_level += 1
newline_indent = '
\
n
' +
(' ' * (_indent * _current_indent_level))
newline_indent = '
\
n
' +
_indent * _current_indent_level
item_separator = _item_separator + newline_indent
item_separator = _item_separator + newline_indent
yield newline_indent
yield newline_indent
else:
else:
...
@@ -383,7 +386,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
...
@@ -383,7 +386,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield chunk
yield chunk
if newline_indent is not None:
if newline_indent is not None:
_current_indent_level -= 1
_current_indent_level -= 1
yield '
\
n
' +
(' ' * (_indent * _current_indent_level))
yield '
\
n
' +
_indent * _current_indent_level
yield '}'
yield '}'
if markers is not None:
if markers is not None:
del markers[markerid]
del markers[markerid]
...
...
Lib/json/tests/test_indent.py
View file @
b643ef8f
...
@@ -10,32 +10,36 @@ class TestIndent(TestCase):
...
@@ -10,32 +10,36 @@ class TestIndent(TestCase):
expect
=
textwrap
.
dedent
(
"""
\
expect
=
textwrap
.
dedent
(
"""
\
[
[
[
\
t
[
"blorpie"
\
t
\
t
"blorpie"
],
\
t
],
[
\
t
[
"whoops"
\
t
\
t
"whoops"
],
\
t
],
[],
\
t
[],
"d-shtaeou",
\
t
"d-shtaeou",
"d-nthiouh",
\
t
"d-nthiouh",
"i-vhbjkhnth",
\
t
"i-vhbjkhnth",
{
\
t
{
"nifty": 87
\
t
\
t
"nifty": 87
},
\
t
},
{
\
t
{
"field": "yes",
\
t
\
t
"field": "yes",
"morefield": false
\
t
\
t
"morefield": false
}
\
t
}
]"""
)
]"""
)
d1
=
json
.
dumps
(
h
)
d1
=
json
.
dumps
(
h
)
d2
=
json
.
dumps
(
h
,
indent
=
2
,
sort_keys
=
True
,
separators
=
(
','
,
': '
))
d2
=
json
.
dumps
(
h
,
indent
=
2
,
sort_keys
=
True
,
separators
=
(
','
,
': '
))
d3
=
json
.
dumps
(
h
,
indent
=
'
\
t
'
,
sort_keys
=
True
,
separators
=
(
','
,
': '
))
h1
=
json
.
loads
(
d1
)
h1
=
json
.
loads
(
d1
)
h2
=
json
.
loads
(
d2
)
h2
=
json
.
loads
(
d2
)
h3
=
json
.
loads
(
d3
)
self
.
assertEquals
(
h1
,
h
)
self
.
assertEquals
(
h1
,
h
)
self
.
assertEquals
(
h2
,
h
)
self
.
assertEquals
(
h2
,
h
)
self
.
assertEquals
(
d2
,
expect
)
self
.
assertEquals
(
h3
,
h
)
self
.
assertEquals
(
d2
,
expect
.
expandtabs
(
2
))
self
.
assertEquals
(
d3
,
expect
)
Misc/NEWS
View file @
b643ef8f
...
@@ -59,6 +59,9 @@ Core and Builtins
...
@@ -59,6 +59,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #5729: json.dumps() now supports using a string such as '\t'
for pretty-printing multilevel objects.
- Issue #10253: FileIO leaks a file descriptor when trying to open a file
- Issue #10253: FileIO leaks a file descriptor when trying to open a file
for append that isn't seekable. Patch by Brian Brazil.
for append that isn't seekable. Patch by Brian Brazil.
...
...
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