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

How to deserialize a struct? #423

Closed
Angelo13C opened this issue Oct 18, 2022 · 1 comment · Fixed by #424
Closed

How to deserialize a struct? #423

Angelo13C opened this issue Oct 18, 2022 · 1 comment · Fixed by #424

Comments

@Angelo13C
Copy link

Hi guys. I am serializing a struct manually (because of something bigger I need to do) and the result of the serialization is this:

SerializeDyn(
    type: "engine_utils::types::registry::tests::Player",
)

Now I need to deserialize it back manually.. but I'm having some problems trying to understand how to do that.. Because for now I have this piece of code:

fn deserialize<'de>(deserializer: ron::Deserializer<'de, &mut Vec<u8>>)
{
    deserializer.deserialize_struct("SerializeDyn", &["type"], SerializeDynVisitor);
}

struct SerializeDynVisitor;
impl<'de> Visitor<'de> for SerializeDynVisitor {
    type Value = Box<dyn Any>;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "a serialize dyn struct")
    }
    
    //Apparently when I deserialize a struct, this is what ends up being called in the visitor
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where A: serde::de::MapAccess<'de>
    {
        let entry = map.next_entry::<&str, String>()?;    //Here I get the error!
        
        todo!()
    }
}

But when I do map.next_entry... I get this error:

Err(
    InvalidValueForType {
        expected: "a borrowed string",
        found: "the string \"type\"",
    },
)

What does this mean? I can't do map.next_entry::<String, String>()? because it panics saying: IdDeserializer may only be used for identifiers but I don't understand what I should do to read the value of the field.

@juntyr
Copy link
Member

juntyr commented Oct 18, 2022

Thanks for coming across this issue! This was indeed an issue on our end that serde-derived code never triggered. Whether one should be able to deserialise a field name into a String is another question - so far we still do not support that but we easily could in the future. However, your use case should now (once I've merged the PR) work on the main branch (see the new 423_de_borrowed_identifier.rs test).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants