Skip to content

Deserializing runtime types #2609

@racradi

Description

@racradi

Sorry if this is not the right place to ask, but I've been stuck with this problem for a while and I'm not sure if it's possible to achieve this using Serde.

I have what is effectively a dynamic (runtime) representation of a struct. I want to use this representation to deserialize some data. To my understanding, the typical solution would be to treat it as a map and use deserialize_map alongside a custom Visitor, however I cannot do that as the serialization format I'm using has length-prefixed maps. The data I'm interested in deserializing does not treat these types as maps, and so there's no length prefix. Although serialize_struct is pretty much what I want, I can't use that either as the names of the members are not &'static str.

More concretely, here's a condensed example:

enum Kind {
    Struct,
    Number,
}

// Using this representation of the struct, I know the types and
// names of each member.
struct MyStructRepr {
    name: String,
    members: Vec<(String, Kind)>,
}

// The actual data into which I want to deserialize the input.
enum Data {
    Struct {
        members: Vec<Data>,
    },
    Number(usize),
    String(String),
}

Is this doable with Serde?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions