1

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),
        }
    }
}
4

1 回答 1

2

尝试以下操作:

serializer.serialize_none()

这序列化None(而不是Some

于 2017-10-31T03:00:09.763 回答