Skip to content

Commit

Permalink
refactor: replace orjson support with msgspec (#2170)
Browse files Browse the repository at this point in the history
* refactor: replace orjson support with msgspec

* style(pre-commit): auto fixes from pre-commit.com hooks

* chore: add to changelog

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lala Sabathil <lala@pycord.dev>
  • Loading branch information
3 people committed Jul 14, 2023
1 parent 6ccfd7f commit 9926ef5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -94,6 +94,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2087](https://github.com/Pycord-Development/pycord/pull/2087))
- Typehinted `command_prefix` and `help_command` arguments properly.
([#2099](https://github.com/Pycord-Development/pycord/pull/2099))
- Replace `orjson` support with `msgspec` support.
([#2170](https://github.com/Pycord-Development/pycord/pull/2170))

### Removed

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -85,7 +85,7 @@ Optional Packages

* `PyNaCl <https://pypi.org/project/PyNaCl/>`__ (for voice support)
* `aiodns <https://pypi.org/project/aiodns/>`__, `brotlipy <https://pypi.org/project/brotlipy/>`__, `cchardet <https://pypi.org/project/cchardet/>`__ (for aiohttp speedup)
* `orjson <https://pypi.org/project/orjson/>`__ (for json speedup)
* `msgspec <https://pypi.org/project/msgspec/>`__ (for json speedup)

Please note that while installing voice support on Linux, you must install the following packages via your preferred package manager (e.g. ``apt``, ``dnf``, etc) BEFORE running the above commands:

Expand Down
12 changes: 6 additions & 6 deletions discord/utils.py
Expand Up @@ -66,11 +66,11 @@
from .errors import HTTPException, InvalidArgument

try:
import orjson
import msgspec
except ModuleNotFoundError:
HAS_ORJSON = False
HAS_MSGSPEC = False
else:
HAS_ORJSON = True
HAS_MSGSPEC = True


__all__ = (
Expand Down Expand Up @@ -662,12 +662,12 @@ def _bytes_to_base64_data(data: bytes) -> str:
return fmt.format(mime=mime, data=b64)


if HAS_ORJSON:
if HAS_MSGSPEC:

def _to_json(obj: Any) -> str: # type: ignore
return orjson.dumps(obj).decode("utf-8")
return msgspec.json.encode(obj).decode("utf-8")

_from_json = orjson.loads # type: ignore
_from_json = msgspec.json.decode # type: ignore

else:

Expand Down
2 changes: 1 addition & 1 deletion requirements/speed.txt
@@ -1,2 +1,2 @@
orjson>=3.5.4
msgspec~=0.17.0
aiohttp[speedups]

0 comments on commit 9926ef5

Please sign in to comment.