Skip to content

Cannot round-trip bare enums #40

@ronnodas

Description

@ronnodas

The following code fails at the unwrap() (with the error 'Syntax(invalid type: string "Variant", expected enum Enum, 0, 0)').

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
enum Enum {
    Variant,
}

#[test]
fn round_trip() {
    let variant = Enum::Variant;
    let string = serde_hjson::to_string(&variant).unwrap();
    assert_eq!(&string, "Variant");
    let round_trip: Enum = serde_hjson::from_str(&string).unwrap(); // fails here
    assert_eq!(variant, round_trip);
}

A workaround I have found is to have the enum be internally tagged, (i.e. add #[serde(tag="tag")] or equivalent), which does allow the round-trip to work, but changes the serialized format so may not work for all use cases.

This is on serde-hjson = 1.1.0 with serde = 1.0.228.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions