Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ def test_document_none_type():
assert doc.as_obj == [None]


def test_document_dict_type():
"""
Ensure we can load and dump the dict type.
"""
doc = Document('{"a": "b"}')
assert doc.dumps() == '{"a":"b"}'
assert doc.as_obj == {'a': 'b'}

doc = Document({"a": "b"})
assert doc.dumps() == '{"a":"b"}'
assert doc.as_obj == {'a': 'b'}

with pytest.raises(TypeError) as exc:
doc = Document({1: 'b'})
assert exc.value.args[0] == 'Dictionary keys must be strings'

with pytest.raises(TypeError) as exc:
doc = Document({'\ud83d\ude47': 'foo'})
assert exc.value.args[0] == 'Dictionary keys must be strings'


def test_document_get_pointer():
"""
Ensure JSON pointers work.
Expand Down
7 changes: 7 additions & 0 deletions yyjson/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ static inline yyjson_mut_val *mut_primitive_to_element(
while (PyDict_Next(obj, &i, &key, &value)) {
Py_ssize_t str_len;
const char *str = PyUnicode_AsUTF8AndSize(key, &str_len);
if (yyjson_unlikely(str == NULL)) {
PyErr_Format(PyExc_TypeError,
"Dictionary keys must be strings",
Py_TYPE(obj)->tp_name
);
return NULL;
}
object_value = mut_primitive_to_element(self, doc, value);
if (yyjson_unlikely(object_value == NULL)) {
return NULL;
Expand Down
Loading