Skip to main content

empty_string_as_none

Function empty_string_as_none 

Source
pub fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where D: Deserializer<'de>, T: FromStr, T::Err: Display,
Expand description

Serde 反序列化装饰器,将空字符串映射为 None

§类型参数

  • D: 反序列化器类型
  • T: 目标类型,必须实现 FromStr

§返回

  • Ok(Some(T)): 非空字符串,成功解析为 T
  • Ok(None): 空字符串或 null
  • Err: 解析失败

§示例

use serde::{Deserialize};

#[derive(Deserialize)]
struct MyStruct {
    #[serde(deserialize_with = "axum_bootstrap::util::json::empty_string_as_none")]
    value: Option<i32>,
}