Skip to content

Commit b99f42b

Browse files
committed
Pass indent to ext module
1 parent 4c84106 commit b99f42b

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

pandas/_libs/src/ujson/python/objToJSON.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,8 @@ PyObject *objToJSON(PyObject *self, PyObject *args, PyObject *kwargs) {
23972397
pyEncoder.defaultHandler = odefHandler;
23982398
}
23992399

2400+
encoder->indent = indent;
2401+
24002402
pyEncoder.originalOutputFormat = pyEncoder.outputFormat;
24012403
PRINTMARK();
24022404
ret = JSON_EncodeObject(oinput, encoder, buffer, sizeof(buffer));

pandas/core/generic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,7 @@ def to_excel(self, excel_writer, sheet_name="Sheet1", na_rep="",
21782178
def to_json(self, path_or_buf=None, orient=None, date_format=None,
21792179
double_precision=10, force_ascii=True, date_unit='ms',
21802180
default_handler=None, lines=False, compression='infer',
2181-
index=True):
2181+
index=True, indent=0):
21822182
"""
21832183
Convert the object to a JSON string.
21842184
@@ -2260,6 +2260,11 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
22602260
22612261
.. versionadded:: 0.23.0
22622262
2263+
indent : integer, default 0
2264+
Length of whitespace used to indent each record.
2265+
2266+
.. versionadded:: 0.25.0
2267+
22632268
Returns
22642269
-------
22652270
None or str
@@ -2325,7 +2330,7 @@ def to_json(self, path_or_buf=None, orient=None, date_format=None,
23252330
force_ascii=force_ascii, date_unit=date_unit,
23262331
default_handler=default_handler,
23272332
lines=lines, compression=compression,
2328-
index=index)
2333+
index=index, indent=indent)
23292334

23302335
def to_hdf(self, path_or_buf, key, **kwargs):
23312336
"""

pandas/io/json/json.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
def to_json(path_or_buf, obj, orient=None, date_format='epoch',
3434
double_precision=10, force_ascii=True, date_unit='ms',
3535
default_handler=None, lines=False, compression='infer',
36-
index=True):
36+
index=True, indent=0):
3737

3838
if not index and orient not in ['split', 'table']:
3939
raise ValueError("'index=False' is only valid when 'orient' is "
@@ -59,7 +59,7 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
5959
obj, orient=orient, date_format=date_format,
6060
double_precision=double_precision, ensure_ascii=force_ascii,
6161
date_unit=date_unit, default_handler=default_handler,
62-
index=index).write()
62+
index=index, indent=indent).write()
6363

6464
if lines:
6565
s = _convert_to_line_delimits(s)
@@ -78,7 +78,8 @@ def to_json(path_or_buf, obj, orient=None, date_format='epoch',
7878

7979
class Writer:
8080
def __init__(self, obj, orient, date_format, double_precision,
81-
ensure_ascii, date_unit, index, default_handler=None):
81+
ensure_ascii, date_unit, index, default_handler=None,
82+
indent=0):
8283
self.obj = obj
8384

8485
if orient is None:
@@ -91,6 +92,7 @@ def __init__(self, obj, orient, date_format, double_precision,
9192
self.date_unit = date_unit
9293
self.default_handler = default_handler
9394
self.index = index
95+
self.indent = indent
9496

9597
self.is_copy = None
9698
self._format_axes()
@@ -101,18 +103,20 @@ def _format_axes(self):
101103
def write(self):
102104
return self._write(self.obj, self.orient, self.double_precision,
103105
self.ensure_ascii, self.date_unit,
104-
self.date_format == 'iso', self.default_handler)
106+
self.date_format == 'iso', self.default_handler,
107+
self.indent)
105108

106109
def _write(self, obj, orient, double_precision, ensure_ascii,
107-
date_unit, iso_dates, default_handler):
110+
date_unit, iso_dates, default_handler, indent):
108111
return dumps(
109112
obj,
110113
orient=orient,
111114
double_precision=double_precision,
112115
ensure_ascii=ensure_ascii,
113116
date_unit=date_unit,
114117
iso_dates=iso_dates,
115-
default_handler=default_handler
118+
default_handler=default_handler,
119+
indent=indent
116120
)
117121

118122

@@ -125,11 +129,11 @@ def _format_axes(self):
125129
"'{orient}'".format(orient=self.orient))
126130

127131
def _write(self, obj, orient, double_precision, ensure_ascii,
128-
date_unit, iso_dates, default_handler):
132+
date_unit, iso_dates, default_handler, indent):
129133
if not self.index and orient == 'split':
130134
obj = {"name": obj.name, "data": obj.values}
131135
return super()._write(obj, orient, double_precision, ensure_ascii,
132-
date_unit, iso_dates, default_handler)
136+
date_unit, iso_dates, default_handler, indent)
133137

134138

135139
class FrameWriter(Writer):
@@ -149,19 +153,20 @@ def _format_axes(self):
149153
"'{orient}'.".format(orient=self.orient))
150154

151155
def _write(self, obj, orient, double_precision, ensure_ascii,
152-
date_unit, iso_dates, default_handler):
156+
date_unit, iso_dates, default_handler, indent):
153157
if not self.index and orient == 'split':
154158
obj = obj.to_dict(orient='split')
155159
del obj["index"]
156160
return super()._write(obj, orient, double_precision, ensure_ascii,
157-
date_unit, iso_dates, default_handler)
161+
date_unit, iso_dates, default_handler, indent)
158162

159163

160164
class JSONTableWriter(FrameWriter):
161165
_default_orient = 'records'
162166

163167
def __init__(self, obj, orient, date_format, double_precision,
164-
ensure_ascii, date_unit, index, default_handler=None):
168+
ensure_ascii, date_unit, index, default_handler=None,
169+
indent=0):
165170
"""
166171
Adds a `schema` attribute with the Table Schema, resets
167172
the index (can't do in caller, because the schema inference needs
@@ -170,7 +175,7 @@ def __init__(self, obj, orient, date_format, double_precision,
170175
"""
171176
super().__init__(obj, orient, date_format, double_precision,
172177
ensure_ascii, date_unit, index,
173-
default_handler=default_handler)
178+
default_handler=default_handler, indent=indent)
174179

175180
if date_format != 'iso':
176181
msg = ("Trying to write with `orient='table'` and "
@@ -211,9 +216,9 @@ def __init__(self, obj, orient, date_format, double_precision,
211216
self.index = index
212217

213218
def _write(self, obj, orient, double_precision, ensure_ascii,
214-
date_unit, iso_dates, default_handler):
219+
date_unit, iso_dates, default_handler, indent):
215220
data = super()._write(obj, orient, double_precision, ensure_ascii,
216-
date_unit, iso_dates, default_handler)
221+
date_unit, iso_dates, default_handler, indent)
217222
serialized = '{{"schema": {schema}, "data": {data}}}'.format(
218223
schema=dumps(self.schema), data=data)
219224
return serialized

0 commit comments

Comments
 (0)