Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Struct with named fields can be deserialized from sequence #138

Closed
ghost opened this issue Jul 27, 2019 · 4 comments
Closed

Struct with named fields can be deserialized from sequence #138

ghost opened this issue Jul 27, 2019 · 4 comments
Labels

Comments

@ghost
Copy link

ghost commented Jul 27, 2019

This issue in Serde affects Serde Cbor.

Example code adapted to Serde Cbor:

use serde_derive::Deserialize;

#[derive(Debug, Deserialize)]
struct Person {
    first_name: String,
    last_name: String,
}

fn main() {
    let data = serde_cbor::to_vec(&("John", "Doe")).unwrap();
    eprintln!("{:?}", data);
    eprintln!("{:#?}", serde_cbor::from_slice::<Person>(&data));
}
@pyfisch
Copy link
Owner

pyfisch commented Aug 5, 2019

Ok, this comes as a surprise to me.

@pyfisch
Copy link
Owner

pyfisch commented Nov 18, 2019

I am not sure if this is an issue or rather just how serde works. If it causes problems it should be solved on the serde side.

@pyfisch pyfisch closed this as completed Nov 18, 2019
@ghost
Copy link
Author

ghost commented Nov 26, 2019

Even if this is an issue with serde, serde-cbor should have the issue open until it has been fixed upstream.

@pyfisch
Copy link
Owner

pyfisch commented Nov 26, 2019

Fine. I don't think this is an issue but rather a feature of serde to help with the deserialization of some data formats that do not distinguish between arrays and maps. And I don't keep issues around for features people find surprising.

(Btw besides the example with structs and sequences it can be rather surpising how serialized data is deserialized in serde. Serde appears to follow Postel's law "be conservative in what you do, be liberal in what you accept from others" to enable working with various different formats. This can it make it difficult to code against a specification which exactly describes what is allowed and what must not be accepted.)

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

No branches or pull requests

1 participant