From 6088ab1f2cb9a6233f71ced9f71efa67dfd8acae Mon Sep 17 00:00:00 2001 From: Dan Sully Date: Tue, 8 Jul 2025 09:01:56 -0700 Subject: [PATCH 1/7] Add .size() to allow for parsing NDJSON files. --- pyproject.toml | 2 +- tests/test_document.py | 23 +++++++++++++++++++++++ yyjson/__init__.pyi | 1 + yyjson/document.c | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 97fbed6..2cb253f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "yyjson" -version = "4.0.6" +version = "4.0.7" description = "JSON parser & serializer built on yyjson" readme = "README.md" authors = [ diff --git a/tests/test_document.py b/tests/test_document.py index 62afd04..da84e77 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -240,3 +240,26 @@ def test_document_freeze(): doc.freeze() assert doc.is_thawed is False + + +def test_document_size(): + """ + Test the size attribute that returns the size of data read from original JSON input. + """ + # Test with immutable document (created from JSON string) + json_str = '{"hello": "world", "number": 42}' + doc = Document(json_str) + assert doc.size == len(json_str) + + # Test with different sized JSON inputs + small_json = "{}" + doc_small = Document(small_json) + assert doc_small.size == len(small_json) + + large_json = '{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], "count": 2}' + doc_large = Document(large_json) + assert doc_large.size == len(large_json) + + # Test with mutable document (created from Python object) - should return 0 + doc_mutable = Document({"hello": "world"}) + assert doc_mutable.size == 0 diff --git a/yyjson/__init__.pyi b/yyjson/__init__.pyi index fa17364..2978be0 100644 --- a/yyjson/__init__.pyi +++ b/yyjson/__init__.pyi @@ -48,6 +48,7 @@ class Document: def is_thawed(self) -> bool: ... def freeze(self) -> None: ... def thaw(self) -> None: ... + def size(self) -> int: ... def load( fp, diff --git a/yyjson/document.c b/yyjson/document.c index 69c6c5f..fb07b7f 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -597,6 +597,17 @@ static PyObject *Document_is_thawed(DocumentObject *self, void *closure) { return PyBool_FromLong(self->m_doc != NULL); } +/** + * Get the size of data read from the original JSON input. + */ +static PyObject *Document_size(DocumentObject *self, void *closure) { + if (self->i_doc) { + return PyLong_FromSize_t(yyjson_doc_get_read_size(self->i_doc)); + } else { + return PyLong_FromLong(0); + } +} + PyDoc_STRVAR( Document_dumps_doc, "Dumps the document to a string and returns it.\n" @@ -1009,6 +1020,8 @@ static PyGetSetDef Document_members[] = { NULL}, {"is_thawed", (getter)Document_is_thawed, NULL, "Returns whether the Document is thawed/mutable.", NULL}, + {"size", (getter)Document_size, NULL, + "Returns the size of data read from the original JSON input.", NULL}, {NULL} /* Sentinel */ }; From 66b77859fb50c09bfb37b59762f8406baa22df2e Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Thu, 30 Oct 2025 05:57:20 -0400 Subject: [PATCH 2/7] Fix #22 --- .github/workflows/release.yml | 10 +++++----- pyproject.toml | 7 ++++++- yyjson/document.c | 1 - 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1be0fd6..321061a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@v4.1.7 - name: Setting up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: 3.13 @@ -54,12 +54,12 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest, macos-14] - py: ["cp39", "cp310", "cp311", "cp312", "cp313", "pp39", "pp310"] + py: ["cp39", "cp310", "cp311", "cp312", "cp313", "cp314", "pp39", "pp310"] steps: - uses: actions/checkout@v4.1.7 - - uses: actions/setup-python@v5 + - uses: actions/setup-python@v6 name: Setting up Python with: python-version: '3.13' @@ -71,7 +71,7 @@ jobs: platforms: all - name: Build & test wheels - uses: pypa/cibuildwheel@v2.22.0 + uses: pypa/cibuildwheel@v3.2.1 env: CIBW_ARCHS_LINUX: auto aarch64 ppc64le s390x CIBW_ARCHS_MACOS: x86_64 arm64 universal2 @@ -108,7 +108,7 @@ jobs: - uses: actions/checkout@v3 - name: Setting up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v6 with: python-version: 3.13 diff --git a/pyproject.toml b/pyproject.toml index 97fbed6..05208b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "yyjson" -version = "4.0.6" +version = "4.0.7" description = "JSON parser & serializer built on yyjson" readme = "README.md" authors = [ @@ -22,6 +22,7 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Operating System :: OS Independent", @@ -49,3 +50,7 @@ ext-modules = [ ] packages = ["yyjson"] +[tool.uv] +cache-keys = [ + { dir = "yyjson" } +] \ No newline at end of file diff --git a/yyjson/document.c b/yyjson/document.c index 69c6c5f..9607103 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -549,7 +549,6 @@ static int Document_init(DocumentObject *self, PyObject *args, PyObject *kwds) { self->i_doc = yyjson_read_file(str, r_flag, self->alc, &err); Py_XDECREF(as_str); - Py_XDECREF(str); if (!self->i_doc) { PyErr_SetString(PyExc_ValueError, err.msg); From 67b105e8c121efc359df5b099019b539d959a0d0 Mon Sep 17 00:00:00 2001 From: blackout Date: Mon, 21 Apr 2025 16:06:23 -0400 Subject: [PATCH 3/7] Serialize tuples as lists --- tests/test_document.py | 24 ++++++++++++++++++++++++ yyjson/document.c | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tests/test_document.py b/tests/test_document.py index 62afd04..7c53e44 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -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(): """ diff --git a/yyjson/document.c b/yyjson/document.c index 9607103..481b8fe 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -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) { @@ -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; From 803263a6eff098fbed1ce0ad3f9eb69fe7cbb387 Mon Sep 17 00:00:00 2001 From: blackout Date: Mon, 21 Apr 2025 17:01:23 -0400 Subject: [PATCH 4/7] Dict keys must be strings --- tests/test_document.py | 21 +++++++++++++++++++++ yyjson/document.c | 7 +++++++ 2 files changed, 28 insertions(+) diff --git a/tests/test_document.py b/tests/test_document.py index 7c53e44..367b078 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -190,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. diff --git a/yyjson/document.c b/yyjson/document.c index 481b8fe..808f4b4 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -379,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; From e90c426a614cde66693377324dba9bae43ccce1c Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Thu, 30 Oct 2025 05:59:29 -0400 Subject: [PATCH 5/7] Version bump --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 05208b1..24cf916 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "yyjson" -version = "4.0.7" +version = "4.1.0" description = "JSON parser & serializer built on yyjson" readme = "README.md" authors = [ From 5014ac3ea3a13b2fd51cf0b4ce745977a50ba1bd Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Thu, 30 Oct 2025 06:16:08 -0400 Subject: [PATCH 6/7] size -> bytes_read, typing fixes. --- pyproject.toml | 3 ++- tests/test_document.py | 12 ++++++------ yyjson/__init__.pyi | 3 ++- yyjson/document.c | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 24cf916..c13351a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ classifiers = [ "Operating System :: OS Independent", "Topic :: Software Development :: Libraries :: Python Modules", ] +requires-python = ">=3.9" [project.urls] Homepage = "https://github.com/tktech/py_yyjson" @@ -52,5 +53,5 @@ packages = ["yyjson"] [tool.uv] cache-keys = [ - { dir = "yyjson" } + { file = "yyjson/*.c" } ] \ No newline at end of file diff --git a/tests/test_document.py b/tests/test_document.py index 5252751..747f2e6 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -203,11 +203,11 @@ def test_document_dict_type(): assert doc.as_obj == {'a': 'b'} with pytest.raises(TypeError) as exc: - doc = Document({1: 'b'}) + Document({1: 'b'}) assert exc.value.args[0] == 'Dictionary keys must be strings' with pytest.raises(TypeError) as exc: - doc = Document({'\ud83d\ude47': 'foo'}) + Document({'\ud83d\ude47': 'foo'}) assert exc.value.args[0] == 'Dictionary keys must be strings' @@ -294,17 +294,17 @@ def test_document_size(): # Test with immutable document (created from JSON string) json_str = '{"hello": "world", "number": 42}' doc = Document(json_str) - assert doc.size == len(json_str) + assert doc.bytes_read == len(json_str) # Test with different sized JSON inputs small_json = "{}" doc_small = Document(small_json) - assert doc_small.size == len(small_json) + assert doc_small.bytes_read == len(small_json) large_json = '{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], "count": 2}' doc_large = Document(large_json) - assert doc_large.size == len(large_json) + assert doc_large.bytes_read == len(large_json) # Test with mutable document (created from Python object) - should return 0 doc_mutable = Document({"hello": "world"}) - assert doc_mutable.size == 0 + assert doc_mutable.bytes_read == 0 diff --git a/yyjson/__init__.pyi b/yyjson/__init__.pyi index 2978be0..8ecabae 100644 --- a/yyjson/__init__.pyi +++ b/yyjson/__init__.pyi @@ -46,9 +46,10 @@ class Document: ) -> "Document": ... @property def is_thawed(self) -> bool: ... + @property + def bytes_read(self) -> int: ... def freeze(self) -> None: ... def thaw(self) -> None: ... - def size(self) -> int: ... def load( fp, diff --git a/yyjson/document.c b/yyjson/document.c index 8ff8af0..f91e4b9 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -621,7 +621,7 @@ static PyObject *Document_is_thawed(DocumentObject *self, void *closure) { /** * Get the size of data read from the original JSON input. */ -static PyObject *Document_size(DocumentObject *self, void *closure) { +static PyObject *Document_bytes_read(DocumentObject *self, void *closure) { if (self->i_doc) { return PyLong_FromSize_t(yyjson_doc_get_read_size(self->i_doc)); } else { @@ -1041,7 +1041,7 @@ static PyGetSetDef Document_members[] = { NULL}, {"is_thawed", (getter)Document_is_thawed, NULL, "Returns whether the Document is thawed/mutable.", NULL}, - {"size", (getter)Document_size, NULL, + {"bytes_read", (getter)Document_bytes_read, NULL, "Returns the size of data read from the original JSON input.", NULL}, {NULL} /* Sentinel */ }; From 764e4a74228ba6886ae2a1557e39034fcc826a99 Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Thu, 30 Oct 2025 06:23:28 -0400 Subject: [PATCH 7/7] Latest versions of cibuildwheel need to explicitly enable pypy. --- pyproject.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c13351a..d9b568a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,10 @@ ext-modules = [ ] packages = ["yyjson"] +[tool.cibuildwheel] +enable = ["pypy"] + [tool.uv] cache-keys = [ { file = "yyjson/*.c" } -] \ No newline at end of file +]