-
-
Notifications
You must be signed in to change notification settings - Fork 860
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
Add support for custom JSON encoders/decoders. #3300
Conversation
It's getting late over here, still need to figure out how to satisfy mypy with Protocol usage, as |
After wrangling with correct type definitions for **kwargs, I concede defeat and decided to use If there is a better solution for this I'd would be more than happy to learn, this is a little frustrating :) |
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? |
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. |
Thanks @AndreCimander this one is proving a bit of a thorny issue. |
@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) |
Summary
Added customizable JSON encoder/decoder.
Working example (also provided in docs):
Checklist