【问题标题】:Skipping serialization of a variant if its value is empty? (custom serialization)如果变量的值为空,则跳过其序列化? (自定义序列化)
【发布时间】:2017-10-30 16:32:59
【问题描述】:

如果Token 变体的值为空,我想跳过它的序列化。

impl<'a> Serialize for UriParam<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        match self {
            &UriParam::Token(ref token) => {
                if !token.is_empty() {
                    serializer.serialize_some(token)
                } else {
                    // what should I return instead?
                }
            },
            &UriParam::URI(ref uri) => serializer.serialize_some(uri),
            &UriParam::Param(ref option) => serializer.serialize_some(option),
        }
    }
}

【问题讨论】:

    标签: rust serde serde-json


    【解决方案1】:

    尝试以下方法:

    serializer.serialize_none()
    

    This serializes None (as opposed to Some)

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 2020-01-23
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多