serde-double-tag

Crates.ioserde-double-tag
lib.rsserde-double-tag
version0.0.4
sourcesrc
created_at2024-07-31 14:00:36.870398
updated_at2024-08-01 15:07:17.842094
descriptiondouble tagged enum representation for `serde`
homepage
repositoryhttps://github.com/de-vri-es/serde-double-tag
max_upload_size
id1320947
size19,599
Maarten de Vries (de-vri-es)

documentation

https://docs.rs/serde-double-tag

README

serde-double-tag

This crates provides derive macros for a double tagged enum representation for serde. It is basically a combination of externally and adjecently tagged enums.

If you enable the schemars feature, the crate also exposes a derive macro for the schemars::JsonSchema trait.

For example, consider this enum:

#[derive(serde_double_tag::Deserialize, serde_double_tag::Serialize)]
#[serde(tag = "species")]
#[serde(rename_all = "snake_case")]
enum Friend {
  Human {
    name: String,
    hobbies: Vec<String>,
  },
  Dog {
    name: String,
    color: String,
  }
}

A Friend::Human will be serialized as:

{
  "species": "human",
  "human": {
    "name": "Zohan",
    "hobbies": ["hair dressing"],
  }
}

Similarly, a Friend::Dog will be serialized as:

{
  "species": "dog",
  "dog": {
    "name": "Scrappy",
    "color": "white and gray",
  }
}

This enum representation could be useful if you want data for the different variants to co-exist in a single file or in your database. Since each variant uses a different field name, they will never conflict. And since there is still a separate field for the enum tag, you can still known which variant is actually active.

Currently supported serde attributes:

  • `#[serde(rename = "...")]
  • `#[serde(rename_all = "...")]
  • `#[serde(rename_all_fields = "...")]
  • `#[serde(deny_unknown_fields = "...")]
Commit count: 0

cargo fmt