Skip to content

Commit

Permalink
map serialization testing
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Feb 24, 2024
1 parent d31fab9 commit 50136a0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ impl<'de, 'a> de::MapAccess<'de> for MapDeserializer<'a, 'de> {
.saturating_sub(self.de.input.len());
let key_bytes = &previous_input_slice[..key_len];
if let Some(previous_key_bytes) = self.previous_key_bytes {
if previous_key_bytes >= key_bytes {
return Err(Error::NonCanonicalMap);
}
// if previous_key_bytes >= key_bytes {
// return Err(Error::NonCanonicalMap);
// }
}
self.remaining = remaining;
self.previous_key_bytes = Some(key_bytes);
Expand Down
53 changes: 50 additions & 3 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ where
type SerializeTuple = Self;
type SerializeTupleStruct = Self;
type SerializeTupleVariant = Self;
type SerializeMap = MapSerializer<'a, W>;
type SerializeMap = DirectMapSerializer<'a, W>;
type SerializeStruct = Self;
type SerializeStructVariant = Self;

Expand Down Expand Up @@ -375,8 +375,14 @@ where
Ok(self)
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(MapSerializer::new(self))
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
if let Some(len) = len {
DirectMapSerializer::new(self, len)
} else {
Err(Error::MissingLen)
}

// Ok(MapSerializer::new(self))
}

fn serialize_struct(
Expand Down Expand Up @@ -482,6 +488,47 @@ where
}
}

struct DirectMapSerializer<'a, W: ?Sized> {
serializer: Serializer<'a, W>,
}

impl<'a, W: ?Sized> DirectMapSerializer<'a, W> {
fn new(mut serializer: Serializer<'a, W>, len: usize) -> Result<Self>
where W: std::io::Write {
serializer.output_seq_len(len)?;
Ok(DirectMapSerializer {
serializer,
})
}
}

impl<'a, W> ser::SerializeMap for DirectMapSerializer<'a, W>
where
W: ?Sized + std::io::Write,
{
type Ok = ();
type Error = Error;

fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
key.serialize(Serializer::new(self.serializer.output, self.serializer.max_remaining_depth))
}

fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.serializer.output, self.serializer.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}


#[doc(hidden)]
struct MapSerializer<'a, W: ?Sized> {
serializer: Serializer<'a, W>,
Expand Down

0 comments on commit 50136a0

Please sign in to comment.