iddm / serde-aux

An auxiliary serde library providing helpful functions for serialisation and deserialisation for containers, struct fields and others.
MIT License
152 stars 26 forks source link

Question: how to apply deserialize_struct_case_insensitive to a struct? #7

Open jlgerber opened 4 years ago

jlgerber commented 4 years ago

I am a bit confused by deserialize_struct_case_insensitive. It is categorized as a container_attribute in serde-aux's docs. However, the example seems to show it used as a field attribute. If it is a container_attribute, should I be able to do something like this?

#[derive(PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(deserialize_with = "deserialize_struct_case_insensitive")]
pub struct Manifest {
    name: String,
    version: String,
}
iddm commented 4 years ago

Hi @jlgerber ! Yes, the name is, unfortunately, confusing, I had plans to reorganise everything differently, but it is still yet to happen. And your thoughts are absolutely logical and correct and I apologise for having named it that way.

I'll see, perhaps, it is possible to rewrite it so that it can indeed be applied to a struct.

jlgerber commented 4 years ago

Thank you for the prompt reply.

Fortunately, I realized that i only really had to support keys with first letters capitalized so i used aliases.

iddm commented 4 years ago

You have still made a reasonable point. As I have wanted for so long to fix this, I now have another good reason for that. So, I'll close it and let you know once I do that.

Thank you for reaching out and contributing!

0cv commented 1 year ago

@vityafx - do you have any updates?

It seems to me that deserialize_with is a field only attribute, but it doesn't apply to the field itself, but to the struct of that field (assuming the field has a struct type).

I'm not sure how this would be possible to apply this to the parent struct

EDIT: arguably, using flatten, such as #[serde(flatten, deserialize_with = "deserialize_struct_case_insensitive")] could do the trick, but it's just a bit awkward having to declare an additional struct, then using flatten ...

iddm commented 1 year ago

It seems to me that deserialize_with is a field only attribute, but it doesn't apply to the field itself, but to the struct of that field (assuming the field has a struct type). That is correct.

I'm not sure how this would be possible to apply this to the parent struct I can't think of any way for this to happen.

I agree with the flatten thing. Unfortunately, there will be no other way possible for this, I think, with how the serde crate is written and what it allows to do.

What kind of an update were you looking for? Just renaming/reorganisation of the crate's content or, perhaps, anything else?

0cv commented 1 year ago

You had written before:

You have still made a reasonable point. As I have wanted for so long to fix this, I now have another good reason for that. So, I'll close it and let you know once I do that.

And I was hopeful, that maybe you had found a way to avoid using a nested struct. Or maybe your comment applied to clarifying the usage?

iddm commented 1 year ago

You had written before:

You have still made a reasonable point. As I have wanted for so long to fix this, I now have another good reason for that. So, I'll close it and let you know once I do that.

And I was hopeful, that maybe you had found a way to avoid using a nested struct. Or maybe your comment applied to clarifying the usage?

Yes, so unfortunately it was more, if not only, clarifying the situation and the absence of a good workaround.