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

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
@T-256
Copy link
Contributor

T-256 commented Sep 23, 2024

Also, see #717 and its linked pr: #3199

@tomchristie
Copy link
Member

Appreciate that there's clearly demand for something like this, tho have been clear that a global registry approach isn't going to be accepted. Can we close this off, and continue pushing for design-discussion-first approachs?

@AndreCimander
Copy link
Author

Ah, sorry, haven't seen that comment, should have searched more diligently.

Bummer, well, yeah, I can understand your point. But I really would have favored a global registry call in my projects :)

A customized Client solution is fine as well (but it feels a bit off needing to customize 3 classes just for a simple json callable change).

I'll close this PR then.

@tomchristie
Copy link
Member

Thanks @AndreCimander this one is proving a bit of a thorny issue.

@gtors
Copy link

gtors commented Oct 1, 2024

A customized Client solution is fine as well (but it feels a bit off needing to customize 3 classes just for a simple json callable change).

@AndreCimander Try httpj 😎 It's my fork with similar functionality:

import httpj
import msgspec
import typing


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

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

client = httpj.Client(json_serialize=custom_json_encoder, json_deserialize=custom_json_decoder)

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.

4 participants