-
Notifications
You must be signed in to change notification settings - Fork 22
/
deserialize_str_to_number.rs
24 lines (23 loc) · 964 Bytes
/
deserialize_str_to_number.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use serde::de::Error as SerdeError;
use serde::{Deserialize, Deserializer};
use serde_json::{Number, Value};
/// Deserializes a `String` to a `Number`
///
/// # Arguments
/// * `deserializer` - The deserializer instance from which to read the JSON value
///
/// # Returns a `Result` that is:
/// - `Ok(Number)` when the input is either a valid `Number` string or a JSON `Number`
/// - `Err(D::Error)` when there's an error from the deserializer if the input is neither a stringified number nor a direct `Number`,
/// or if the `String` cannot be parsed into a `Number`
pub fn deserialize_str_to_number<'de, D>(deserializer: D) -> Result<Number, D::Error>
where
D: Deserializer<'de>,
{
let value: Value = Deserialize::deserialize(deserializer)?;
match value {
Value::String(s) => s.parse::<Number>().map_err(SerdeError::custom),
Value::Number(n) => Ok(n),
_ => Err(SerdeError::custom("Expected a string or a number")),
}
}