Skip to content

BUG: Attributes skipped when serialising plain Python objects to JSON (#42768) #42931

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

Merged
merged 7 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ I/O
- Bug in :func:`read_excel` attempting to read chart sheets from .xlsx files (:issue:`41448`)
- Bug in :func:`json_normalize` where ``errors=ignore`` could fail to ignore missing values of ``meta`` when ``record_path`` has a length greater than one (:issue:`41876`)
- Bug in :func:`read_csv` with multi-header input and arguments referencing column names as tuples (:issue:`42446`)
- Bug in :func:`Series.to_json` and :func:`DataFrame.to_json` where some attributes were skipped when serialising plain Python objects to JSON (:issue:`42768`, :issue:`33043`)
-

Period
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,6 @@ int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {

GET_TC(tc)->itemName = itemName;
GET_TC(tc)->itemValue = itemValue;
GET_TC(tc)->index++;

itemName = attr;
break;
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,3 +1769,18 @@ def test_to_json_multiindex_escape(self):
"\"(Timestamp('2017-01-23 00:00:00'), 'bar')\":true}"
)
assert result == expected

def to_json_series_of_objects(self):
class _TestObject:
def __init__(self, a, b, _c, d):
self.a = a
self.b = b
self._c = _c
self.d = d

def e(self):
return 5

# JSON keys should be all non-callable non-underscore attributes, see GH-42768
series = Series([_TestObject(a=1, b=2, _c=3, d=4)])
assert json.loads(series.to_json()) == {"0": {"a": 1, "b": 2, "d": 4}}
15 changes: 15 additions & 0 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,21 @@ def my_obj_handler(_):
ujson.encode(obj_list, default_handler=str)
)

def test_encode_object(self):
class _TestObject:
def __init__(self, a, b, _c, d):
self.a = a
self.b = b
self._c = _c
self.d = d

def e(self):
return 5

# JSON keys should be all non-callable non-underscore attributes, see GH-42768
test_object = _TestObject(a=1, b=2, _c=3, d=4)
assert ujson.decode(ujson.encode(test_object)) == {"a": 1, "b": 2, "d": 4}


class TestNumpyJSONTests:
@pytest.mark.parametrize("bool_input", [True, False])
Expand Down