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

Commit ed2594e

Browse files
likebreathroypat
authored andcommitted
Fix manual (De)Serialize implementations with serde_json
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>
1 parent d45810f commit ed2594e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 78.57,
2+
"coverage_score": 81.25,
33
"exclude_path": ".*bindings\\.rs",
44
"crate_features": "fam-wrappers,serde"
55
}

src/serialize.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ macro_rules! serde_impls {
4242
backing[..limit].copy_from_slice(&bytes[..limit]);
4343
Ok(backing)
4444
}
45+
46+
fn visit_seq<A: serde::de::SeqAccess<'a>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
47+
let mut backing = [0u8; std::mem::size_of::<$typ>()];
48+
49+
for backing_byte in &mut backing {
50+
let Some(byte) = seq.next_element()? else { break };
51+
52+
*backing_byte = byte;
53+
}
54+
55+
Ok(backing)
56+
}
4557
}
4658

4759
let backing = deserializer.deserialize_bytes(BytesVisitor)?;

0 commit comments

Comments
 (0)