Open jsouto18 opened 3 months ago
When using an untagged enum to handle generic Serialization + Deserialization the deserialization will always fail and the cause seems to be the numeric keys of BTreeMap. If using the struct directly everything works as expected.
Serialization + Deserialization
BTreeMap
Can this be related to https://github.com/serde-rs/serde/issues/1183? Also not sure if this is a serde or serde_json issue so apologies if this is the wrong repo.
serde
serde_json
Minimum Reproducible Example:
use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "form", content = "data")] enum StoredSerializationForm<'a, T> { Compressed(Vec<u8>), #[serde(untagged)] #[serde(bound(deserialize = "Ptr<'a,T>: Deserialize<'de>"))] Uncompressed(Ptr<'a, T>), } #[derive(Debug, Serialize)] #[serde(untagged)] enum Ptr<'a, T> { Ref(&'a T), Owned(T), } impl<'de, 'a, T> Deserialize<'de> for Ptr<'a, T> where T: Deserialize<'de>, { fn deserialize<D>(deserializer: D) -> std::prelude::v1::Result<Self, D::Error> where D: serde::Deserializer<'de>, { Deserialize::deserialize(deserializer).map(Ptr::Owned) } } #[derive(Debug, Serialize, Deserialize)] struct TestStruct { ctx: BTreeMap<u64, u64>, } #[cfg(test)] mod tests { use super::*; #[test] fn test_fails() { let mut ctx = BTreeMap::new(); ctx.insert(1, 42); ctx.insert(2, 42); let test_struct = TestStruct { ctx }; let data = StoredSerializationForm::Uncompressed(Ptr::Ref(&test_struct)); let serialized = serde_json::to_vec(&data).unwrap(); let _: StoredSerializationForm<TestStruct> = serde_json::from_slice(&serialized).unwrap(); } #[test] fn test_works() { let mut ctx = BTreeMap::new(); ctx.insert(1, 42); ctx.insert(2, 42); let test_struct = TestStruct { ctx }; let serialized = serde_json::to_vec(&test_struct).unwrap(); let _: TestStruct = serde_json::from_slice(&serialized).unwrap(); } }
When using an untagged enum to handle generic
Serialization + Deserialization
the deserialization will always fail and the cause seems to be the numeric keys ofBTreeMap
. If using the struct directly everything works as expected.Can this be related to https://github.com/serde-rs/serde/issues/1183? Also not sure if this is a
serde
orserde_json
issue so apologies if this is the wrong repo.Minimum Reproducible Example: