Skip to content
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

Don't prepend marker for deserialization of root struct #48

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ use std::io;

pub struct Deserializer<'b> {
buffer: &'b mut dyn io::BufRead,
read_header: bool,
}

impl<'b> Deserializer<'b> {
pub fn new(buffer: &'b mut dyn io::BufRead) -> Self {
Self { buffer }
Self {
buffer,
read_header: false,
}
}
}

Expand Down Expand Up @@ -262,6 +266,11 @@ impl<'de, 'a, 'b> serde::Deserializer<'de> for &'a mut Deserializer<'b> {
where
V: Visitor<'de>,
{
if !self.read_header {
self.read_header = true;
return visitor.visit_map(MapAccess::with_varint_encoded_fields(self)?);
}

let marker = self.read_marker()?;
self.dispatch_based_on_marker(marker, visitor)
}
Expand Down Expand Up @@ -479,8 +488,7 @@ impl<'de, 'a, 'b> serde::Deserializer<'de> for &'a mut Deserializer<'b> {
where
V: Visitor<'de>,
{
self.read_expected_marker(MARKER_SINGLE_STRUCT)?;
visitor.visit_map(MapAccess::with_varint_encoded_fields(self)?)
self.deserialize_any(visitor)
}

fn deserialize_struct<V>(
Expand All @@ -492,8 +500,7 @@ impl<'de, 'a, 'b> serde::Deserializer<'de> for &'a mut Deserializer<'b> {
where
V: Visitor<'de>,
{
self.read_expected_marker(MARKER_SINGLE_STRUCT)?;
visitor.visit_map(MapAccess::with_varint_encoded_fields(self)?)
self.deserialize_any(visitor)
}

fn deserialize_enum<V>(
Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ where
return Err(Error::missing_header_bytes());
}

// Monero encodes markers after each key and before each value which
// means if the value is a struct the marker will need to be read.
//
// However the parent struct does not have any marker so we add it here.
// see: https://github.com/monero-rs/monero-epee-bin-serde/issues/36
//
let mut bytes = &[&[MARKER_SINGLE_STRUCT.to_byte()], bytes].concat()[..];

let mut deserializer = Deserializer::new(&mut bytes);

T::deserialize(&mut deserializer)
Expand Down