Skip to content

Commit 43ab17b

Browse files
committed
Expanded test and clean implementation
1 parent 1bcf354 commit 43ab17b

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

pandas/_libs/src/ujson/lib/ultrajsonenc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name,
10031003

10041004
Buffer_AppendCharUnchecked(enc, '{');
10051005
Buffer_AppendIndentNewlineUnchecked (enc);
1006-
Buffer_AppendIndentUnchecked (enc, enc->level + 2);
1006+
Buffer_AppendIndentUnchecked (enc, enc->level);
10071007

10081008
while (enc->iterNext(obj, &tc)) {
10091009
if (count > 0) {
@@ -1012,7 +1012,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name,
10121012
Buffer_AppendCharUnchecked(enc, ' ');
10131013
#endif
10141014
Buffer_AppendIndentNewlineUnchecked (enc);
1015-
Buffer_AppendIndentUnchecked (enc, enc->level + 2);
1015+
Buffer_AppendIndentUnchecked (enc, enc->level);
10161016
}
10171017

10181018
iterObj = enc->iterGetValue(obj, &tc);
@@ -1025,7 +1025,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name,
10251025

10261026
enc->iterEnd(obj, &tc);
10271027
Buffer_AppendIndentNewlineUnchecked (enc);
1028-
Buffer_AppendIndentUnchecked (enc, enc->level + 1);
1028+
Buffer_AppendIndentUnchecked (enc, enc->level);
10291029
Buffer_AppendCharUnchecked(enc, '}');
10301030
break;
10311031
}

pandas/tests/io/json/test_pandas.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,22 +1648,24 @@ def test_tuple_labels(self, orient, expected):
16481648
result = df.to_json(orient=orient)
16491649
assert result == expected
16501650

1651-
def test_to_json_indent(self):
1651+
@pytest.mark.parametrize("indent", [1, 2, 4])
1652+
def test_to_json_indent(self, indent):
16521653
# GH 12004
16531654
df = pd.DataFrame([
16541655
['foo', 'bar'], ['baz', 'qux']
16551656
], columns=['a', 'b'])
16561657

1657-
result = df.to_json(indent=4)
1658-
expected = """{
1659-
"a":{
1660-
"0":"foo",
1661-
"1":"baz"
1662-
},
1663-
"b":{
1664-
"0":"bar",
1665-
"1":"qux"
1666-
}
1667-
}"""
1658+
result = df.to_json(indent=indent)
1659+
spaces = " " * indent
1660+
expected = """{{
1661+
"a":{{
1662+
{spaces}"0":"foo",
1663+
{spaces}"1":"baz"
1664+
{spaces}}},
1665+
"b":{{
1666+
{spaces}"0":"bar",
1667+
{spaces}"1":"qux"
1668+
{spaces}}}
1669+
}}""".format(spaces=spaces)
16681670

16691671
assert result == expected

0 commit comments

Comments
 (0)