Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Fix manual (De)Serialize implementations with serde_json
Browse files Browse the repository at this point in the history
The new Deserializer implementation (#107) does not work with the
'serde_json' crate, particularly `serde_json::{to_string,
from_str}`. The new implementation always expects `byte array` for
deserilaiztion. Meanwhile, the 'serde_json' crate always (de)serializes
`String` as `Vector` [1][2] which is treated as `sequence` (e.g. not
'byte array') by default for (de)serialization [3].

To fix this issue, this patch extends the new Deserializer
implementation to support both `byte array` and `sequence`.

[1] https://github.com/serde-rs/json/blob/cc7a1608c9bb7736c884926e016421af41a1ebe7/src/ser.rs#L2168-L2175
[2] https://github.com/serde-rs/json/blob/cc7a1608c9bb7736c884926e016421af41a1ebe7/src/de.rs#L30-L39
[3] https://serde.rs/data-model.html#types

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
Signed-off-by: Bo Chen <chen.bo@intel.com>
  • Loading branch information
likebreath authored and roypat committed Sep 3, 2024
1 parent d45810f commit ed2594e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 78.57,
"coverage_score": 81.25,
"exclude_path": ".*bindings\\.rs",
"crate_features": "fam-wrappers,serde"
}
12 changes: 12 additions & 0 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ macro_rules! serde_impls {
backing[..limit].copy_from_slice(&bytes[..limit]);
Ok(backing)
}

fn visit_seq<A: serde::de::SeqAccess<'a>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut backing = [0u8; std::mem::size_of::<$typ>()];

for backing_byte in &mut backing {
let Some(byte) = seq.next_element()? else { break };

*backing_byte = byte;
}

Ok(backing)
}
}

let backing = deserializer.deserialize_bytes(BytesVisitor)?;
Expand Down

0 comments on commit ed2594e

Please sign in to comment.