-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization of Vec<Option<String>> implicitly skips None variants #81
Comments
Hey @zexa, thanks for opening an issue. You're right, that's not I case I had considered previously. The problem is that we have generic logic for handling Do you have an existing API you are trying to conform to? If so, can you provide information on what values that would expect to get? Or how would you expect this to work? I could imagine special-casing this so you woudl get What might be the simplest approach for you, as a workaround, would be to implement custom serialize/deserialize logic to do something similar? |
For example, I think a custom serializer could be: fn serialize_opt_Str<S>(opt_str_array: &[Option<String>], S) -> Result<S::Ok, S::Error> where S: Serializer {
let mut seq = serializer.serialize_seq(Some(self.len()))?;
for e in opt_str_array {
match e {
Some(s) => seq.serialize_element(s)?,
None = seq.serialize_element(&())?,
}
seq.serialize_element(e)?;
}
seq.end()
} along with: #[derive(Debug, Clone, Serialize, Deserialize)]
struct Example {
#[serde(serialize_with="serialize_opt_str")]
opt_str_array: Vec<Option<String>>,
} |
It's likely that we'll stick to your suggested approach, which is to use a custom serializer/deserializer, but to be honest I'd like something generic too - it would be an overall improvement to the lib. Personally, I'd be satisfied with the provided serializer/deserializer.
I'm hesitant to provide the exact code as it's proprietary. The example provides enough context. Unless you need some specific information? In which case order is important here.
If you consider a On the other hand, I did consider an interesting approach - |
Seems like we're skipping serialization on None values, which is kind of unexpected and causes wrong ser/de when None values are required (i.e. to handle in backend). See bellow example:
I guess representing None values is an issue. Unclear what kind of string should be used for them. At the very least I'd expect the deserializer to know that if there's
opt_str_array[1]
andopt_str_array[0]
was skipped, that must mean thatopt_str_array[0]
was None.Additionally, I suppose it would be benefitial to know the amount of items in such cases too. I.e. if it was
opt_str_array: vec![None, Some("abc".to_string(), None)]
, we could doopt_str_array[1]=abc&opt_str_array[2]
whereopt_str_array[2]
would be used to denote the size of the vec.The text was updated successfully, but these errors were encountered: