2020-03-30 06:39:46 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-08-03 09:47:15 +02:00
|
|
|
use serde::{Deserialize as DeTrait, Deserializer};
|
2020-04-08 02:05:07 +02:00
|
|
|
pub(crate) use serde_aux::field_attributes::{
|
|
|
|
deserialize_bool_from_anything, deserialize_number_from_string,
|
|
|
|
};
|
2020-03-30 06:39:46 +02:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2021-04-18 10:29:20 +02:00
|
|
|
use crate::timestamp::TimestampSecs;
|
|
|
|
|
2021-01-25 06:54:44 +01:00
|
|
|
/// Note: if you wish to cover the case where a field is missing, make sure you also
|
|
|
|
/// use the `serde(default)` flag.
|
2020-03-30 06:39:46 +02:00
|
|
|
pub(crate) fn default_on_invalid<'de, T, D>(deserializer: D) -> Result<T, D::Error>
|
|
|
|
where
|
|
|
|
T: Default + DeTrait<'de>,
|
|
|
|
D: serde::de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let v: Value = DeTrait::deserialize(deserializer)?;
|
|
|
|
Ok(T::deserialize(v).unwrap_or_default())
|
|
|
|
}
|
2020-08-03 09:47:15 +02:00
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
pub(crate) fn deserialize_int_from_number<'de, T, D>(deserializer: D) -> Result<T, D::Error>
|
2020-08-03 09:47:15 +02:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
2020-08-07 07:02:03 +02:00
|
|
|
T: serde::Deserialize<'de> + FromI64,
|
2020-08-03 09:47:15 +02:00
|
|
|
{
|
|
|
|
#[derive(DeTrait)]
|
|
|
|
#[serde(untagged)]
|
2020-08-07 07:02:03 +02:00
|
|
|
enum IntOrFloat {
|
|
|
|
Int(i64),
|
2020-08-03 09:47:15 +02:00
|
|
|
Float(f64),
|
|
|
|
}
|
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
match IntOrFloat::deserialize(deserializer)? {
|
|
|
|
IntOrFloat::Float(f) => Ok(T::from_i64(f as i64)),
|
|
|
|
IntOrFloat::Int(i) => Ok(T::from_i64(i)),
|
2020-08-03 09:47:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It may be possible to use the num_traits crate instead in the future.
|
2020-08-07 07:02:03 +02:00
|
|
|
pub(crate) trait FromI64 {
|
|
|
|
fn from_i64(val: i64) -> Self;
|
2020-08-03 09:47:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
impl FromI64 for i32 {
|
|
|
|
fn from_i64(val: i64) -> Self {
|
2020-08-03 09:47:15 +02:00
|
|
|
val as Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
impl FromI64 for u32 {
|
|
|
|
fn from_i64(val: i64) -> Self {
|
|
|
|
val.max(0) as Self
|
2020-08-03 09:47:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
impl FromI64 for i64 {
|
|
|
|
fn from_i64(val: i64) -> Self {
|
|
|
|
val
|
2020-08-03 09:47:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 07:02:03 +02:00
|
|
|
impl FromI64 for TimestampSecs {
|
|
|
|
fn from_i64(val: i64) -> Self {
|
2020-08-03 09:47:15 +02:00
|
|
|
TimestampSecs(val as i64)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 06:54:44 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2021-04-18 10:29:20 +02:00
|
|
|
use super::*;
|
|
|
|
|
2021-01-25 06:54:44 +01:00
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
|
|
struct MaybeInvalid {
|
|
|
|
#[serde(deserialize_with = "default_on_invalid", default)]
|
|
|
|
field: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_or_missing() {
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::from_str::<MaybeInvalid>(r#"{"field": 5}"#).unwrap(),
|
|
|
|
MaybeInvalid { field: Some(5) }
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::from_str::<MaybeInvalid>(r#"{"field": "5"}"#).unwrap(),
|
|
|
|
MaybeInvalid { field: None }
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::from_str::<MaybeInvalid>(r#"{"another": 5}"#).unwrap(),
|
|
|
|
MaybeInvalid { field: None }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|