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

What if I don't want the packed serialization? #112

Closed
thanatos opened this issue Jun 1, 2019 · 6 comments
Closed

What if I don't want the packed serialization? #112

thanatos opened this issue Jun 1, 2019 · 6 comments

Comments

@thanatos
Copy link

thanatos commented Jun 1, 2019

I'm trying to avoid having a format that inadvertently works. While avoiding serialization is a matter of just not calling the _packed functions, deserialization seems to implicitly always use it.

E.g., coding against a spec, I do not want other, incorrect (according to the spec, which might be agnostic to Rust/serde_cbor) to successfully deserialize, lest someone thinks that format is supported.

@pyfisch
Copy link
Owner

pyfisch commented Jun 1, 2019

I'm trying to avoid having a format that inadvertently works. While avoiding serialization is a matter of just not calling the _packed functions, deserialization seems to implicitly always use it.

Your assessment is unfortunately correct. In the next version I want to change the default serialization of enums anyway (#85) so I might as well make de-serialization more restrictive.

@pyfisch pyfisch mentioned this issue Jun 10, 2019
@pyfisch
Copy link
Owner

pyfisch commented Jun 13, 2019

@dtolnay Serde deserializes maps with numbered keys like {0: "foo", 1: 42} to structures like struct Foo {name: String, age: u32}, is there a way to opt-out of this and only accept named keys?

@baloo
Copy link
Contributor

baloo commented Jun 13, 2019

From my understanding, this is only code generated in the serde_derive deserializer. You may remove that behavior by manually implementing the Deserializer.

I also have another use-case where by spec the format is packed, with custom indexes:
https://github.com/mozilla/authenticator-rs/blob/df28de2/src/ctap2/commands.rs#L1178
https://github.com/mozilla/authenticator-rs/blob/df28de2/cose/src/lib.rs#L174

I'd be interested to get support for that in serde_derive, that would save a ton of repetitive code.

@dtolnay
Copy link

dtolnay commented Jun 14, 2019

is there a way to opt-out of this and only accept named keys?

One way would be that if the Deserializer is called with deserialize_struct then use an implementation of MapAccess whose next_key only allows string keys. In serde_json we do the same thing. The relevant code is https://github.com/serde-rs/json/blob/v1.0.39/src/de.rs#L1763-L1768

@baloo
Copy link
Contributor

baloo commented Jun 14, 2019

I implemented that in #126, thank you @dtolnay !

@baloo
Copy link
Contributor

baloo commented Jun 26, 2019

this issue is fixed in master and should be closed

@pyfisch pyfisch closed this as completed Jun 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants