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

Document (more prominently?) that serde_json allows sequences to be deserialized into structs #2438

Closed
tbu- opened this issue May 1, 2023 · 3 comments

Comments

@tbu-
Copy link
Contributor

tbu- commented May 1, 2023

Last time I saw this was https://old.reddit.com/r/rust/comments/1332t9b/ji7oo8m/, but I've also seen it earlier and found it surprising both times.

Basically both {"hello": "test", "value": 42} (expected) and ["test", 42] deserialize perfectly fine into the following struct:

#[derive(Serialize, Deserialize)]
pub struct HelloParams {
    hello: String, 
    value: i32,
}

Since this is not mentioned anywhere, this can accidentally become a used API when serde is used to parse JSON (Hyrum's law).

It would be nice if this could be featured somewhere prominently in the docs. I couldn't actually find it there, even looking for it.

@Mingun
Copy link
Contributor

Mingun commented May 28, 2023

Probably serde_json should not call visit_seq if deserialize_map / deserialize_struct was requested by the type. Then deserialization structs from JSON arrays would be impossible. That also can be a configurable behavior of serde_json deserializer.

@Mingun
Copy link
Contributor

Mingun commented May 28, 2023

Actually, this is duplicate of #1587.

@oli-obk oli-obk closed this as completed May 30, 2023
@tbu-
Copy link
Contributor Author

tbu- commented May 30, 2023

I don't see how this is a duplicate, @oli-obk. This is about documenting behavior of serde_json, not about changing it. The other issue asks for behavioral changes that are likely backward incompatible and thus unlikely to happen.

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

No branches or pull requests

3 participants