enser 0.1.0

Enum serialization with tag
Documentation

✒️ enser

Crates.io docs.rs CI Coverage Status

Enum Serialization with Tag

Usage

Add the following to Cargo.toml

enser = "0.1.0"
  #[derive(Debug, Deserialize, Serialize)]
+ #[enser::enser]
  enum MyEnum {
      Tbd,
      None,
      Some(u32),
      Named { value: u32 },
  }

Rationale

Given the following enum:

#[derive(Debug, Deserialize, Serialize)]
enum MyEnum {
    Tbd,
    None,
    Some(u32),
    Named { value: u32 },
}

When serializing Vec<MyEnum>, the output is:

# serde_yaml
my_enums:
- Tbd
- None
- !Some 123
- !Named
  value: 456

# serde_json
{
  "without_tuple": [
    "Tbd",
    "None",
    { "Some": 123 }
    { "Named": { "value": 456 } }
  ]
}

When the #[enser::enser] attribute is added:

  #[derive(Debug, Deserialize, Serialize)]
+ #[enser::enser]
  enum MyEnum { .. }

The output is:

# serde_yaml -- a !Tag is used for each variant
my_enums:
- !Tbd null
- !None null
- !Some 123
- !Named
  value: 456

# serde_json -- every variant is an object
{
  "my_enums": [
    { "Tbd": null },
    { "None": null },
    { "Some": 123 },
    { "Named": { "value": 456 } }
  ]
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.