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 2 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`)
- Some attributes were skipped when serialising plain Python objects to JSON (:issue:`42768`, :issue:`33043`)
-

Period
Expand Down
42 changes: 14 additions & 28 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,37 +882,33 @@ void Dir_iterEnd(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {

int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
PyObject *obj = (PyObject *)_obj;
PyObject *itemValue = GET_TC(tc)->itemValue;
PyObject *itemName = GET_TC(tc)->itemName;
PyObject *attr;
PyObject *attrName;
char *attrStr;

if (PyErr_Occurred() || ((JSONObjectEncoder *)tc->encoder)->errorMsg) {
return 0;
}

if (itemValue) {
if (GET_TC(tc)->itemValue) {
Py_DECREF(GET_TC(tc)->itemValue);
GET_TC(tc)->itemValue = itemValue = NULL;
GET_TC(tc)->itemValue = NULL;
}

if (itemName) {
if (GET_TC(tc)->itemName) {
Py_DECREF(GET_TC(tc)->itemName);
GET_TC(tc)->itemName = itemName = NULL;
GET_TC(tc)->itemName = NULL;
}

for (; GET_TC(tc)->index < GET_TC(tc)->size; GET_TC(tc)->index++) {
attrName = PyList_GET_ITEM(GET_TC(tc)->attrList, GET_TC(tc)->index);
attr = PyUnicode_AsUTF8String(attrName);
attrStr = PyBytes_AS_STRING(attr);
PyObject *attrName =
PyList_GET_ITEM(GET_TC(tc)->attrList, GET_TC(tc)->index);
PyObject *attr = PyUnicode_AsUTF8String(attrName);
char *attrStr = PyBytes_AS_STRING(attr);

if (attrStr[0] == '_') {
Py_DECREF(attr);
continue;
}

itemValue = PyObject_GetAttr(obj, attrName);
PyObject *itemValue = PyObject_GetAttr(obj, attrName);
if (itemValue == NULL) {
PyErr_Clear();
Py_DECREF(attr);
Expand All @@ -925,25 +921,15 @@ int Dir_iterNext(JSOBJ _obj, JSONTypeContext *tc) {
continue;
}

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

itemName = attr;
break;
}

if (itemName == NULL) {
GET_TC(tc)->index = GET_TC(tc)->size;
GET_TC(tc)->itemValue = NULL;
return 0;
return 1;
}

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

return 1;
GET_TC(tc)->index = GET_TC(tc)->size;
GET_TC(tc)->itemValue = NULL;
return 0;
}

JSOBJ Dir_iterGetValue(JSOBJ Py_UNUSED(obj), JSONTypeContext *tc) {
Expand Down
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):
# Keys should be all non-callable non-underscore attributes, see GH-42768
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

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