Skip to content

JSON data with extra fields accepted into structs? #3352

Answered by robjtede
tyrsday29 asked this question in General
Discussion options

You must be logged in to vote

Unlike what is possible in JavaScript, having extra JSON fields is not going to cause any security issues in a Rust app. However, you can opt-in to disallowing them using the standard serde deny_unknown_fields attribute.

You can also choose to collect the unknown fields if you want to inspect them for some reason by adding an extra field to your struct:

#[derive(Debug, Deserialize)]
struct Foo {
  // ...

  #[serde(flatten)]
  extra_fields: HashMap<String, serde_json::Value>,
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tyrsday29
Comment options

Answer selected by robjtede
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants