-
Notifications
You must be signed in to change notification settings - Fork 556
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 alternate encodings of bytes types #656
Conversation
#[cfg(feature = "bytes_mode")] | ||
#[derive(Clone, Eq, PartialEq, Debug, Hash)] | ||
#[non_exhaustive] | ||
pub enum BytesMode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too happy about having this in lib.rs
, but no existing module seems like a good place.
@@ -76,3 +77,9 @@ raw_value = [] | |||
# overflow the stack after deserialization has completed, including, but not | |||
# limited to, Display and Debug and Drop impls. | |||
unbounded_depth = [] | |||
|
|||
# Support alternate encoding modes for bytes. Available on Rust 1.40+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Version requirement due to https://github.com/serde-rs/json/pull/656/files#diff-b4aea3e418ccdb71239b96952d9cddb6R470
I don't agree entirely; I haven't encountered a scenario where I have to implement a specific protocol and efficiently support multiple data formats simultaneously. My focus when implementing a protocol (de)serializer is making it compatible with that specific protocol, and I am fine with creating custom As an example, I've recently been implementing a game data parser for one of my projects. I wanted to pick out the relevant bits from a dump of the game data, and so I made a parser for that ( |
@dtolnay ping for review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I would prefer not to build this into serde_json in this way, but I opened dtolnay/request-for-implementation#48 to propose how a separate library could wrap our serializer/deserializer (or any other serde format's) to expose control over the representation of the bytes type.
JSON does not natively support binary data.
serde_json
currently just supports integer arrays and a deserialization mode for raw strings. However, protocols using JSON specify their own mechanisms to handle binary data in JSON, and it's likely not to be whatserde_json
currently does.How types in the serde data model are to be encoded should be specified by the (de)serializer, not the type. Suggestions such as #360 (comment) are therefore not appropriate. By specifying the encoding that way in the type, you can't (de)serialize that type anymore with a (de)serializer that does natively support bytes (such as CBOR).
This PR introduces the concept of alternate encodings for bytes types. When creating a JSON (de)serializer, you can specify the mode you want. The default mode is still integer arrays. This PR adds base64-encoding as an option. All changes can be enabled with features and should be backwards-compatible and zero-cost for existing users. The functionality is thoroughly documented with the
BytesMode
type.There are a couple of TODO items but I wanted to gather feedback before continuing the work.
TODO:
Value as Deserializer
cc @jseyfried