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

Tuple struct does not respect skip_serializing #1048

Closed
dtolnay opened this issue Sep 9, 2017 · 3 comments
Closed

Tuple struct does not respect skip_serializing #1048

dtolnay opened this issue Sep 9, 2017 · 3 comments
Assignees

Comments

@dtolnay
Copy link
Member

dtolnay commented Sep 9, 2017

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

#[derive(Serialize, Debug)]
struct S(usize, #[serde(skip)] usize);

fn main() {
    let s = S(0, 0);
    let j = serde_json::to_string(&s).unwrap();
    println!("{}", j);
}
[0,0]

Open question: whether a tuple struct with all but one skipped field should be serialized as a tuple struct of length 1 or a newtype struct.

@konstin
Copy link

konstin commented Oct 17, 2017

Newtype, please. The current behavior causes problems when using PhantomData<T>.

In my case I want to add another type to my custom url type, which can also be deserialized.

#[derive(Deserialize, Serialize)]
pub struct CustomUrl<T: DeserializeOwned> {
    #[serde(with = "url_serde")]
    url: Url,
    #[serde(skip)]
    url_type: PhantomData<T>
}

Is there any way to tell serde to treat the above as a newtype struct? The above code is currently failing as serde expects a tuple where there's actually a string.

@durka
Copy link

durka commented Oct 29, 2017

I hit this today trying to make a serializable enum with tuple struct variants containing unserializable fields. I can't do it because #[skip] is ignored.

@dtolnay dtolnay self-assigned this May 7, 2018
@dtolnay
Copy link
Member Author

dtolnay commented May 8, 2018

Fixed in b4e51fc. It will continue to serialize as a tuple even if there is only one field. #1054 is tracking an attribute that would make types with a single serialized field serialize just like the underlying field.

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

No branches or pull requests

3 participants