How to skip some bytes in when deserliaize?

I need to deserialize some data using serde, currently I have implemented the serde::de::Visitor trait for the struct that needs to be deserialized. However, there are some unknown fields in the data, I can calculate the length of these fields to skip them, I saw someone recommend using the IgnoreAny type:

_ => {
    let _ : serde::de::IgnoredAny = map.next_value()?;
}

But I need to implement the following function:

fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
    V: de::Visitor<'de>,
{
}

I just want to simply skip these bytes here, and I don't want to perform any deserialization operations. How can I achieve this?

These statements contradict each other. If you are implementing Visitor, then you are implementing a data structure. However, deserialize_ignored_any is a function on the trait Deserializer, which means you are implementing a serialization format. Which one is it? I suspect the recommendation of just using IgnoredAny is right, and you don't actually need to implement a custom deserializer.

1 Like

I use a custom Deserializer to deserialize data, and some of the structures still need to be deserialized manually. So here we have both Visitor and deserialize_ignored_any

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.