-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
Tagged enum support / JSON interoperability #272
Comments
I found the error is coming from this code in the |
Is there any update on this? Here is another minimal example which breaks: #[test]
fn test() {
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(tag = "type", content = "data")]
enum MyEnum {
VariantA,
}
let serialized = bincode::serialize(&MyEnum::VariantA).unwrap();
let deserialized = bincode::deserialize::<MyEnum>(&serialized).unwrap();
assert_eq!(deserialized, MyEnum::VariantA);
}
|
@NyxCode - that is an adjacently tagged enum, in the serde parlance. I have also bumped into this and devised a partial workaround, with the test above extended. Remarks:
|
Unfortunately bincode cannot support |
I know that this is effectively more of a serde issue than a bincode one, but I admit that it's my main gripe with the framework as a whole at the moment. The default externally tagged enums are annoying to work with in JSON, and if you want to use bincode as well then you have no choice. If only I could opt in the feature only for JSON, that'd effectively solve the issue for me. But as far as I can tell it's not really possible? |
@simias yeah, that's not really possible and is an issue with the serde framework itself. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm currently using
serde_json
for simple, debuggable manipulation in a web-based frontend, and I'm hoping to reuse the same datatypes for IPC in the context of a higher-performance application connected via a websocket. I'd like to usebincode
for that purpose, but I'm running into issues getting the two to play nicely together regarding tagged enums.For context, here's a simplified example:
Using the highlighted
serde(tag = ...)
attributes above, as suggested by the documentation here, results in easily usable, descriptive JSON like{"type":"SomeData","variant":"A"}
. Without the tag attributes, I instead get{"SomeData":"A"}
, which is pretty annoying to classify in the Javascript side of things.bincode
, on the other hand, can only deserialize the version without tag attributes. When they're added like in the example above, I get a pretty uninformativeDeserializeAnyNotSupported
error at expect number 4.I assume this is unintended behavior? Even if there's a technical reason the deserialization can't work this way, the serialization should probably be prevented in the first place, and a better error message would help.
The text was updated successfully, but these errors were encountered: