-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Add Indent Support in to_json #28130
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
Changes from 7 commits
bb9c174
4c84106
b99f42b
367b494
1bcf354
43ab17b
592be66
5c0e8a3
d11e47f
21672ed
79c1cbc
0007e34
abdd27f
2da6fbf
a4f740a
cd0c9e6
c740359
2b5cb50
638d055
b870585
ba7f044
df589e3
517377b
4aec9d7
1aa424d
c896b8a
b046061
ae93309
f037d05
ccb9823
7d757e4
95251b1
65315c3
9827a94
0b440e0
0024f41
4869425
f03f05f
b894b8c
c4dba2e
dc68364
6da8684
dab8df1
b679fee
966fadb
c8efda6
5067eb7
f376f12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2249,6 +2249,7 @@ def to_json( | |
lines=False, | ||
compression="infer", | ||
index=True, | ||
indent=0, | ||
): | ||
""" | ||
Convert the object to a JSON string. | ||
|
@@ -2329,6 +2330,11 @@ def to_json( | |
|
||
.. versionadded:: 0.23.0 | ||
|
||
indent : integer, default 0 | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Length of whitespace used to indent each record. | ||
|
||
.. versionadded:: 1.0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this match the semantics of the stdlib, e.g. default is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this only accepts int for now and 0 does not insert new lines. This matches ujson behavior instead of stdlib There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be possible to support this in the long term I just think will take a little bit of effort to bridge the gap with our vendored ujson. Thinking for now it might make the most sense to change the signature to I think that should avoid a deprecation cycle in the future for |
||
|
||
Returns | ||
------- | ||
None or str | ||
|
@@ -2401,6 +2407,7 @@ def to_json( | |
lines=lines, | ||
compression=compression, | ||
index=index, | ||
indent=indent, | ||
) | ||
|
||
def to_hdf(self, path_or_buf, key, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ def to_json( | |
lines=False, | ||
compression="infer", | ||
index=True, | ||
indent=0, | ||
): | ||
|
||
if not index and orient not in ["split", "table"]: | ||
|
@@ -76,6 +77,7 @@ def to_json( | |
date_unit=date_unit, | ||
default_handler=default_handler, | ||
index=index, | ||
indent=indent, | ||
).write() | ||
|
||
if lines: | ||
|
@@ -94,6 +96,7 @@ def to_json( | |
|
||
|
||
class Writer: | ||
|
||
def __init__( | ||
self, | ||
obj, | ||
|
@@ -104,6 +107,7 @@ def __init__( | |
date_unit, | ||
index, | ||
default_handler=None, | ||
indent=0, | ||
): | ||
self.obj = obj | ||
|
||
|
@@ -117,6 +121,7 @@ def __init__( | |
self.date_unit = date_unit | ||
self.default_handler = default_handler | ||
self.index = index | ||
self.indent = indent | ||
|
||
self.is_copy = None | ||
self._format_axes() | ||
|
@@ -133,6 +138,7 @@ def write(self): | |
self.date_unit, | ||
self.date_format == "iso", | ||
self.default_handler, | ||
self.indent, | ||
) | ||
|
||
def _write( | ||
|
@@ -144,6 +150,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
): | ||
return dumps( | ||
obj, | ||
|
@@ -153,6 +160,7 @@ def _write( | |
date_unit=date_unit, | ||
iso_dates=iso_dates, | ||
default_handler=default_handler, | ||
indent=indent, | ||
) | ||
|
||
|
||
|
@@ -175,6 +183,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
): | ||
if not self.index and orient == "split": | ||
obj = {"name": obj.name, "data": obj.values} | ||
|
@@ -186,6 +195,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
) | ||
|
||
|
||
|
@@ -220,6 +230,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
): | ||
if not self.index and orient == "split": | ||
obj = obj.to_dict(orient="split") | ||
|
@@ -232,6 +243,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
) | ||
|
||
|
||
|
@@ -248,13 +260,15 @@ def __init__( | |
date_unit, | ||
index, | ||
default_handler=None, | ||
indent=0, | ||
): | ||
""" | ||
Adds a `schema` attribute with the Table Schema, resets | ||
the index (can't do in caller, because the schema inference needs | ||
to know what the index is, forces orient to records, and forces | ||
date_format to 'iso'. | ||
""" | ||
|
||
super().__init__( | ||
obj, | ||
orient, | ||
|
@@ -264,6 +278,7 @@ def __init__( | |
date_unit, | ||
index, | ||
default_handler=default_handler, | ||
indent=indent, | ||
) | ||
|
||
if date_format != "iso": | ||
|
@@ -315,6 +330,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent, | ||
): | ||
data = super()._write( | ||
obj, | ||
|
@@ -324,6 +340,7 @@ def _write( | |
date_unit, | ||
iso_dates, | ||
default_handler, | ||
indent | ||
) | ||
serialized = '{{"schema": {schema}, "data": {data}}}'.format( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this was creating a formatted string from individual dumps of the JSON data. To get indent to work with that formatted string adds a lot of code, so instead I put everything into an OrderedDict and am letting the JSON encoder do the heavy lifting. This fixed a bug as indicated in the whatsnew but also shed some light on #28256 which I think is best tackled separately. Note that this also changes the whitespace in use for the table format, which was inconsistent before anyway. The JSON encoder doesn't put a space after each colon whereas this formatted string did. Whitespace is insignificant in JSON so I don't think this really needs a whatsnew mention but can be convinced otherwise |
||
schema=dumps(self.schema), data=data | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add type hints when modifying code?