Function activitypub_federation::protocol::helpers::deserialize_one_or_many
source · pub fn deserialize_one_or_many<'de, T, D>(
deserializer: D
) -> Result<Vec<T>, D::Error>where
T: Deserialize<'de>,
D: Deserializer<'de>,
Expand description
Deserialize JSON single value or array into Vec.
Useful if your application can handle multiple values for a field, but another federated platform only sends a single one.
#[derive(serde::Deserialize)]
struct Note {
#[serde(deserialize_with = "deserialize_one_or_many")]
to: Vec<Url>
}
let single: Note = serde_json::from_str(r#"{"to": "https://example.com/u/alice" }"#)?;
assert_eq!(single.to.len(), 1);
let multiple: Note = serde_json::from_str(
r#"{"to": [
"https://example.com/u/alice",
"https://lemmy.ml/u/bob"
]}"#)?;
assert_eq!(multiple.to.len(), 2);
Ok::<(), anyhow::Error>(())