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

Add support for custom JSON encoders/decoders. #3300

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

AndreCimander
Copy link

@AndreCimander AndreCimander commented Sep 11, 2024

Summary

Added customizable JSON encoder/decoder.

Working example (also provided in docs):

import httpx
import msgspec
import typing


encoder = msgspec.json.Encoder()
def custom_json_encoder(json_data: typing.Any) -> bytes:
    return encoder.encode(json_data)

httpx.register_json_encoder(custom_json_encoder)


decoder = msgspec.json.Decoder()
def custom_json_decoder(json_data: bytes, **kwargs: Any) -> bytes:
    return decoder.decode(json_data)

httpx.register_json_decoder(custom_json_decoder)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@AndreCimander AndreCimander marked this pull request as draft September 11, 2024 20:41
@AndreCimander
Copy link
Author

AndreCimander commented Sep 11, 2024

It's getting late over here, still need to figure out how to satisfy mypy with Protocol usage, as mypy_extension.KwArg and mypy_extenions.Arg are deprecated and I don't want to import those (even with if TYPE_CHECKING: import guard).

@AndreCimander
Copy link
Author

After wrangling with correct type definitions for **kwargs, I concede defeat and decided to use # type: ignore.

If there is a better solution for this I'd would be more than happy to learn, this is a little frustrating :)

@AndreCimander AndreCimander marked this pull request as ready for review September 14, 2024 16:46
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.

1 participant