Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom JSON encoders #65

Merged
merged 1 commit into from
Sep 5, 2020
Merged

Conversation

Gelbpunkt
Copy link
Contributor

@Gelbpunkt Gelbpunkt commented Aug 4, 2020

This adds support to do things like bot.wavelink.set_serializer(ujson.dumps) and pretty much any other JSON encoder that dumps to strings or bytes. I did not know a better way to do this other than 3 attributes on all classes to make sure they are being created correctly for any future cases.

I left a note regarding sending bytes, as it would be technically more efficient, yet Lavalink rejects binary traffic for now. Might want to request that in upstream.

I've tested the basic idea of dumping and then using send_str in production with orjson and the send_json from aiohttp pretty much dumps it and sends it then anyways, so there is no overhead created here, yet using a kwarg to the send_json would not allow JSON encoders that return bytes, so I decided for this way.

@Gelbpunkt Gelbpunkt mentioned this pull request Aug 16, 2020
@EvieePy EvieePy merged commit 8e45996 into PythonistaGuild:master Sep 5, 2020
@EvieePy
Copy link
Member

EvieePy commented Sep 5, 2020

Thanks for the PR. It has been rebased.

Was just wondering if there are any checks we can do in the set_serializer function to ensure a valid encoder is passed?

@Gelbpunkt
Copy link
Contributor Author

@EvieePy we could theoretically dump any dictionary, e.g.:

result = self._dumps({'a': 1, 'b': 'hello'})
assert result.decode('utf-8') == '{"a": 1, "b": "hello"}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants