Skip to content
Open
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
45 changes: 45 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ def test_document_boolean_type():
assert doc.dumps() == "[false]"
assert doc.as_obj == [False]

def test_document_list_type():
doc = Document('[1,2,3,4]')
assert doc.dumps() == '[1,2,3,4]'
assert doc.as_obj == [1, 2, 3, 4]

doc = Document([1, 2, 3, 4])
assert doc.dumps() == '[1,2,3,4]'
assert doc.as_obj == [1, 2, 3, 4]

def test_document_tuple_type():
doc = Document(())
assert doc.dumps() == '[]'

doc = Document((1,))
assert doc.dumps() == '[1]'

doc = Document((1, 2, 3, 4))
assert doc.dumps() == '[1,2,3,4]'

doc = Document([(1, 2), (3, 4)])
assert doc.dumps() == '[[1,2],[3,4]]'

doc = Document({'test': (1, 2)})
assert doc.dumps() == '{"test":[1,2]}'

def test_document_none_type():
"""
Expand All @@ -166,6 +190,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
22 changes: 22 additions & 0 deletions yyjson/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ PyTypeObject *type_for_conversion(PyObject *obj) {
return &PyDict_Type;
} else if (obj->ob_type == &PyList_Type) {
return &PyList_Type;
} else if (obj->ob_type == &PyTuple_Type) {
return &PyTuple_Type;
} else if (obj->ob_type == &PyBool_Type) {
return &PyBool_Type;
} else if (obj->ob_type == Py_None->ob_type) {
Expand Down Expand Up @@ -355,6 +357,19 @@ static inline yyjson_mut_val *mut_primitive_to_element(
yyjson_mut_arr_append(val, object_value);
}
return val;
} else if (ob_type == &PyTuple_Type) {
yyjson_mut_val *val = yyjson_mut_arr(doc);
yyjson_mut_val *object_value = NULL;
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(obj); i++) {
object_value = mut_primitive_to_element(self, doc, PyTuple_GET_ITEM(obj, i));

if (yyjson_unlikely(object_value == NULL)) {
return NULL;
}

yyjson_mut_arr_append(val, object_value);
}
return val;
} else if (ob_type == &PyDict_Type) {
yyjson_mut_val *val = yyjson_mut_obj(doc);
yyjson_mut_val *object_value = NULL;
Expand All @@ -364,6 +379,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