Skip to content

Implement serialize_some and possibly serialize_none for serialization_serde #56

Closed

Description

@dnlmlr

Options are currently not supported for serialization in the serialization_serde module. That makes it unnecessarily hard to use a common pattern like this where you have optional values that don't necessarily need to be present and should be left out when they are None:

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "PascalCase")]
pub struct InstalledApp {
    pub display_name: Option<String>,
    pub display_version: Option<String>,
    pub uninstall_string: Option<String>,
}

This works fine for deserialization but runs into the unimplemented serialize_none and serialize_some functions for serialization. The None case can be easily avoided with #[serde(skip_serializing_if = "Option::is_none")], but in case of Some the value would have to be mapped.

It is totally possible that there is reason why the two functions are not implemented but right now I can't think of any reasons.
I would suggest to at least implement serialize_some to simply serialize the T value in Some(T) and I'd probably also implement serialize_none to not do anything.

Let me know what you think and if there are any reasons that made you not implement it. If you want I can also implement this and do a PR

Activity

  1. dnlmlr commented on Jun 26, 2023

    @dnlmlr
    ContributorAuthor

    I implemented this in master...dnlmlr:winreg-rs:feature-serialize_some_none.
    No PR yet just in case I'm missing some major problem.

    Actually while writing this, I noticed that None values would have to be serialized by deleting existing values to stay consistent. If that was / is of concern I'd either implement exactly that (delete existing values in case of None), or just keep None values as unimplemented. Imo Some should still be implemented as in that case there are no consistency problems and devs have to manually handle the None case of Options anyway (due to it not being implemented)

    Edit: both options are now in the branch. The latest commit implements value deletion and the one before that just ignores None values. I personally think it should delete None values, as you could simply use the #[serde(skip_serializing_if = "Option::is_none")] annotation to opt out of this behavior. The other way around there is no such option

  2. gentoo90 commented on Jan 8, 2025

    @gentoo90
    Owner

    Fixed in 0.15.0(winapi)/0.53.0(windows-sys)

  3. gentoo90 commented on Jan 8, 2025

    @gentoo90
    Owner

    Oops, I didn't think of deleting the None. Will have to delete the keys too though.

  4. gentoo90 commented on Jan 8, 2025

    @gentoo90
    Owner

    I opened a separate issue #76 for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions