• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by John Dev


28 Nov, 2024

Updated at 02 Dec, 2024

When is it safe to call unwrap on the result of `serde_json::to_string`?

Let's say I have an instance of this struct:

#[derive(Debug, Serialize, Deserialize)]
pub struct Payload {
    #[serde(skip_serializing)]
    pub k1: String,
    pub k2: u32,
    pub k3: u64,
    #[serde(flatten)]
    pub rest: Value,
}

How safe is it to call serde_json::to_string(o).unwrap()? The documentation states that Serialization can fail if T's implementation of Serialize decides to fail, or if T contains a map with non-string keys. It looks to me like calling unwrap is safe here but I want to be sure? Am I overlooking something?

5 posts - 4 participants

Read full topic