veilid/veilid-core/src/veilid_api/serialize_helpers.rs

25 lines
693 B
Rust
Raw Normal View History

2022-06-08 01:31:05 +00:00
use super::*;
2022-02-07 02:18:42 +00:00
pub fn deserialize_json<'a, T: de::Deserialize<'a>>(
arg: &'a str,
2022-06-08 01:31:05 +00:00
) -> Result<T, super::VeilidAPIError> {
serde_json::from_str(arg).map_err(|e| VeilidAPIError::ParseError {
2022-02-07 02:18:42 +00:00
message: e.to_string(),
value: String::new(),
})
}
pub fn deserialize_opt_json<T: de::DeserializeOwned>(
arg: Option<String>,
2022-06-08 01:31:05 +00:00
) -> Result<T, VeilidAPIError> {
let arg = arg.ok_or_else(|| VeilidAPIError::ParseError {
message: "invalid null string".to_owned(),
2022-02-07 02:18:42 +00:00
value: String::new(),
})?;
deserialize_json(&arg)
}
pub fn serialize_json<T: Serialize>(val: T) -> String {
serde_json::to_string(&val).expect("failed to serialize json value")
}