Closed stanislav-tkach closed 3 years ago
I am afraid these two are incompatible with each other. Serde creates some "aliases" field for each field in a struct for parsing, so it will go through the list of aliases trying to parse the field. I suppose this behaviour is either completely or at least partially disabled by specifying #[serde(deserialize_with)]
because the deserializer, in this case, is different and it can do anything with a field. What it does now with #[serde(deserialize_with = "deserialize_struct_case_insensitive")]
is just before parsing it changes the field name to lowercase and so it doesn't matter what the original name was, it is changed to lower case and then parsed, so the problem you encounter is valid. Unfortunately, there is nothing we can do - it will always be either one deserialiser or another, but it can't be two at the same time.
I might have a look again, perhaps, something has changed since then, but I do not guarantee a solution.
P.S. After a minute of thinking, there might be a solution which is simply not to lowercase a string but to compare it with ignoring the case. Let me try this out.
In the serde-aux
it is impossible to do, unfortunately. I pass a lower-cased string to serde so that it handles all the stuff after. So if I change a field name there and pass it to serde which is instructed to parse a field with a rename
, it simply won't work.
I was working on a patch to serde to allow that actually, but had some issues with that and stopped for some time. I may have another try there, but again, no guarantees. From the last investigation of serde, it simply was coded in a way that was making case-insensitive parsing either very hard or impossible.
Thanks for the quick response! I expected that this issue couldn't be fixed outside of serde. Perhaps documentation can mention this pitfall?
Thanks for the quick response! I expected that this issue couldn't be fixed outside of serde. Perhaps documentation can mention this pitfall?
Sure thing. Thanks for the suggestion.
https://github.com/vityafx/serde-aux/commit/bd5960282a01190a71420244d683b2c2d600d135
Released a version with a documentation note: https://docs.rs/serde-aux/1.0.1/serde_aux/container_attributes/fn.deserialize_struct_case_insensitive.html#notes
If I find out I can do anything else to make this work, I'll comment on this issue and reopen it.
I have encountered strange behavior when
deserialize_struct_case_insensitive
is used in conjunction withserde::rename
. Here is a slightly modified example taken from thedeserialize_struct_case_insensitive
documentation:In this case everything works as expected, but if
#[serde(rename = "fieldname")]
is changed to#[serde(rename = "fieldName")]
then I get the following error: