Skip to content
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

Fix "�� is not JSON serializable" on Python 3 #382

Merged
merged 3 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2

- name: Set up Python 3.8
uses: actions/setup-python@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
python-version: [3.8]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2

- name: pip cache
uses: actions/cache@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- {python-version: 2.7, os: windows-2019}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2

- name: Ubuntu cache
uses: actions/cache@v1
Expand Down
6 changes: 6 additions & 0 deletions python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,13 @@ static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObject
PyErr_Clear();

objRepr = PyObject_Repr(obj);
#if PY_MAJOR_VERSION >= 3
PyObject* str = PyUnicode_AsEncodedString(objRepr, "utf-8", "~E~");
PyErr_Format (PyExc_TypeError, "%s is not JSON serializable", PyString_AS_STRING(str));
Py_XDECREF(str);
#else
PyErr_Format (PyExc_TypeError, "%s is not JSON serializable", PyString_AS_STRING(objRepr));
#endif
Py_DECREF(objRepr);

INVALID:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,33 @@ def test_dumps(test_input, expected):
assert ujson.dumps(test_input) == expected


class SomeObject:
def __repr__(self):
return "Some Object"


if sys.version_info.major == 2:
EMPTY_SET_ERROR = "set([]) is not JSON serializable"
FILLED_SET_ERROR = "set([1, 2, 3]) is not JSON serializable"
else:
EMPTY_SET_ERROR = "set() is not JSON serializable"
FILLED_SET_ERROR = "{1, 2, 3} is not JSON serializable"


@pytest.mark.parametrize(
"test_input, expected_exception, expected_message",
[
(set(), TypeError, EMPTY_SET_ERROR),
({1, 2, 3}, TypeError, FILLED_SET_ERROR),
(SomeObject(), TypeError, "Some Object is not JSON serializable"),
],
)
def test_dumps_raises(test_input, expected_exception, expected_message):
with pytest.raises(expected_exception) as e:
ujson.dumps(test_input)
assert str(e.value) == expected_message


@pytest.mark.parametrize(
"test_input",
[
Expand Down