Skip to content

Commit 4383066

Browse files
committed
making orjson support optional
orjson can cause issues when compiling on Alpine Linux, and also on Read the Docs when building documentation.
1 parent d27c37e commit 4383066

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

docs/src/piccolo/getting_started/installing_piccolo.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Pip
1111

1212
Now install piccolo, ideally inside a `virtualenv <https://docs.python-guide.org/dev/virtualenvs/>`_:
1313

14-
1514
.. code-block:: python
1615
1716
# Optional - creating a virtualenv on Unix:
@@ -21,3 +20,7 @@ Now install piccolo, ideally inside a `virtualenv <https://docs.python-guide.org
2120
2221
# The important bit:
2322
pip install piccolo
23+
24+
# For optional orjson support, which improves JSON serialisation
25+
# performance:
26+
pip install piccolo[orjson]

docs/src/piccolo/query_types/select.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ To return the data as a JSON string:
148148
>>> b.select().output(as_json=True).run_sync()
149149
'[{"name":"Pythonistas","manager":1,"popularity":1000,"id":1},{"name":"Rustaceans","manager":2,"popularity":500,"id":2}]'
150150
151-
Piccolo uses `orjson <https://github.com/ijl/orjson>`_ for JSON serialisation, which is blazing fast, and can handle most Python types, including dates, datetimes, and UUIDs.
151+
Piccolo can use `orjson <https://github.com/ijl/orjson>`_ for JSON serialisation,
152+
which is blazing fast, and can handle most Python types, including dates,
153+
datetimes, and UUIDs. To install Piccolo with orjson support use
154+
`pip install piccolo[orjson]`.
152155

153156
where
154157
~~~~~

piccolo/query/base.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import typing as t
66

77
from asyncpg.pgproto.pgproto import UUID
8-
import orjson as json
98

109
from piccolo.querystring import QueryString
1110
from piccolo.utils.sync import run_sync
@@ -23,17 +22,6 @@ def __exit__(self, exception_type, exception, traceback):
2322
print(f"Duration: {self.end - self.start}s")
2423

2524

26-
def default(obj):
27-
"""
28-
Used for handling edge cases which orjson can't serialise out of the box.
29-
"""
30-
# This is the asyncpg UUID, not the builtin Python UUID, which orjon can
31-
# serialise out of the box.
32-
if isinstance(obj, (UUID, datetime.timedelta)):
33-
return str(obj)
34-
raise TypeError
35-
36-
3725
class Query:
3826

3927
__slots__ = ("table",)
@@ -83,7 +71,14 @@ async def _process_results(self, results):
8371
else:
8472
raw = list(itertools.chain(*[j.values() for j in raw]))
8573
if output._output.as_json:
86-
raw = json.dumps(raw, default=default)
74+
try:
75+
import orjson
76+
except ImportError:
77+
import json
78+
79+
raw = json.dumps(raw, default=str)
80+
else:
81+
raw = orjson.dumps(raw, default=str).decode("utf8")
8782

8883
return raw
8984

requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
aiosqlite==0.15.0
2-
asgiref==3.2.10
2+
asgiref==3.3.0
33
asyncpg==0.21.0
44
black==19.10b0
5-
colorama==0.4.3
6-
orjson==3.4.0
5+
colorama==0.4.4
76
Jinja2==2.11.2
87
targ==0.1.*

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"piccolo": ["py.typed"],
4343
},
4444
install_requires=REQUIREMENTS,
45+
extras_require={"orjson": ["orjson==3.4.1"]},
4546
license="MIT",
4647
classifiers=[
4748
"License :: OSI Approved :: MIT License",

0 commit comments

Comments
 (0)