Skip to content
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

feat req: skip some fields in the struct #29

Open
fslongjin opened this issue Jul 19, 2024 · 4 comments
Open

feat req: skip some fields in the struct #29

fslongjin opened this issue Jul 19, 2024 · 4 comments

Comments

@fslongjin
Copy link

In serde, we can use #[serde(skip)] on fields we don't want to serialize/deserialize.
Here is an example:

#[derive(Serialize, Deserialize)]
struct A {
  xxxx: B,
  #[serde(skip)]
  yyyy: C,
}

In the case above, Type C should not derive Serialize and Deserialize.

@lbirkert
Copy link

This would be a great addition, as sometimes certain fields can be computed from others so storing every field is wasteful (if computing performance is not a factor here). How does it work in practice though? Do I have to provide a value for yyyy while deserializing or does it use the default value? How does serde currently handle it?

@lbirkert
Copy link

lbirkert commented Jul 22, 2024

The semantic way to do this currently would be just to create a seperate struct.

struct A {
  xxxx: B,
  yyyy: C,
}

#[derive(Encode, Decode)]
struct ARaw {
  xxxx: B,
}

impl From<A> for ARaw {
     fn from(val: A) -> Self {
        ARaw { xxxx: val.xxxx }
    }
}

What benefits does creating a separate macro bring except boilerplate reduction? What if I don't want my yyyy to implement default? How would we handle this case?

@finnbear
Copy link
Member

#[serde(skip)] uses Default::default() (can be overridden by #[serde(skip, default = "some_fn")])

@lbirkert
Copy link

seams reasonable then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants