Skip to content

Add dumpb functions to json module #106441

@stalkerg

Description

@stalkerg

This story started when the orjson library became popular. This library is very performant (10x and more) and very correct, especially because it's written on Rust. But one thing was not so shiny - this library used bytes instead str for loads/dumps functions. For this was a reason. Quote from author:

bytes is the correct type for a serialized blob. That alone is good reason but the performance is better for the case worth supporting for the future, something like an API renderer that writes to bytes, or a user who writes an internal library function to serialize, as well.

and I can't disagree, in 100% of cases on the web by HTTP, I receive and return bytes with JSON, not str, same with files, if I read it, it's bytes.
Currently, for the standard json module, after json str is generated, we must encode it, each time. But it's not the main issue - the main issue is integration with 3rd party libraries and frameworks that expect what json module in Python it is standard API and not expected bytes from dumps.
Because loads currently supports bytes, we have inconsistent API - we have the function deserialize from bytes but we have no function to serialize to bytes. It creates interoperability issues.

My proposal is to add dumpb to make API consistent. It does not break backward compatibility but adds more granular and specific API which can be properly used by libraries like web frameworks and HTTP clients.

This proposal is close to such discussion #64036 but it's much more specific and fresh.

Regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions