-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
Allow representation to differ between human-readable and binary formats #790
Comments
I think I'd describe data in this form as a "blob;" formats like JSON can only store arbitrary data as strings, whereas formats like bincode can store any arbitrary bytes. I have an idea for how I'd implement this and I'll post it later once I write it up. |
Perhaps like this?
I'm open for ideas on how to do this without requiring libstd. I'd rather it not be too complicated, though. |
What do you see as the advantages of that approach? Here are some disadvantages:
|
Of course here is the KISS approach: trait Serializer {
/* existing associated types and methods */
#[inline]
fn is_human_readable() -> bool { true }
} fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
if S::is_human_readable() {
serializer.collect_str(self)
} else {
serializer.serialize_bytes(self.as_bytes())
}
} |
I like it, although I think that it'd be nice to have a method that enables the user to test both branches without having to change anything in the serializer. Perhaps we could add a separate |
This implements the KISS suggested in serde-rs#790. It is possible that one of the other approaches may be better but this seemed like the simplest one to reignite som discussion. Personally I find the original suggestion of adding two traits perhaps slightly cleaner in theory but I think it ends up more complicated in the end since the added traits also need to be duplicated to to the `Seed` traits. Closes serde-rs#790
This implements the KISS suggested in serde-rs#790. It is possible that one of the other approaches may be better but this seemed like the simplest one to reignite som discussion. Personally I find the original suggestion of adding two traits perhaps slightly cleaner in theory but I think it ends up more complicated in the end since the added traits also need to be duplicated to to the `Seed` traits. Closes serde-rs#790
@clarcharr
Both |
This implements the KISS suggested in serde-rs/serde#790. It is possible that one of the other approaches may be better but this seemed like the simplest one to reignite som discussion. Personally I find the original suggestion of adding two traits perhaps slightly cleaner in theory but I think it ends up more complicated in the end since the added traits also need to be duplicated to to the `Seed` traits. Closes #790
As an initial brainstormed approach to give an example of what I have in mind:
... and similar for deserialization.
The idea is that chrono::DateTime, std::net::IpAddr etc can serialize themselves as a string in JSON and YAML but compact bytes in Bincode. Currently a SocketAddrV6 takes up to 55 bytes to represent in Bincode but should only be 18 bytes.
The text was updated successfully, but these errors were encountered: