Name Description Size
lib.rs The enum [`Either`] with variants `Left` and `Right` is a general purpose sum type with two cases. [`Either`]: enum.Either.html **Crate features:** * `"use_std"` Enabled by default. Disable to make the library `#![no_std]`. * `"serde"` Disabled by default. Enable to `#[derive(Serialize, Deserialize)]` for `Either` 39825
serde_untagged.rs Untagged serialization/deserialization support for Either<L, R>. `Either` uses default, externally-tagged representation. However, sometimes it is useful to support several alternative types. For example, we may have a field which is generally Map<String, i32> but in typical cases Vec<String> would suffice, too. ```rust # fn main() -> Result<(), Box<dyn std::error::Error>> { use either::Either; use std::collections::HashMap; #[derive(serde::Serialize, serde::Deserialize, Debug)] #[serde(transparent)] struct IntOrString { #[serde(with = "either::serde_untagged")] inner: Either<Vec<String>, HashMap<String, i32>> }; // serialization let data = IntOrString { inner: Either::Left(vec!["Hello".to_string()]) }; // notice: no tags are emitted. assert_eq!(serde_json::to_string(&data)?, r#"["Hello"]"#); // deserialization let data: IntOrString = serde_json::from_str( r#"{"a": 0, "b": 14}"# )?; println!("found {:?}", data); # Ok(()) # } ``` 2028
serde_untagged_optional.rs Untagged serialization/deserialization support for Option<Either<L, R>>. `Either` uses default, externally-tagged representation. However, sometimes it is useful to support several alternative types. For example, we may have a field which is generally Map<String, i32> but in typical cases Vec<String> would suffice, too. ```rust # fn main() -> Result<(), Box<dyn std::error::Error>> { use either::Either; use std::collections::HashMap; #[derive(serde::Serialize, serde::Deserialize, Debug)] #[serde(transparent)] struct IntOrString { #[serde(with = "either::serde_untagged_optional")] inner: Option<Either<Vec<String>, HashMap<String, i32>>> }; // serialization let data = IntOrString { inner: Some(Either::Left(vec!["Hello".to_string()])) }; // notice: no tags are emitted. assert_eq!(serde_json::to_string(&data)?, r#"["Hello"]"#); // deserialization let data: IntOrString = serde_json::from_str( r#"{"a": 0, "b": 14}"# )?; println!("found {:?}", data); # Ok(()) # } ``` 2172