title: Is there is a simpler way to convert a type upon deserialization?
body: <p>Using <a href="https://github.com/serde-rs/json" rel="noreferrer">serde_json</a>, I have JSON objects with <code>String</code>s that I need to convert to floats. I've stumbled upon a custom deserializer solution, but it seems like a hack.  <a href="https://play.integer32.com/?gist=e7fc24feca0d74de3b0a378812fb24a1&amp;version=stable" rel="noreferrer">Here is a working playground example of the code below.</a></p>
  
  <pre><code>#[macro_use]
  extern crate serde_derive;
  extern crate serde;
  extern crate serde_json;
  
  use serde_json::Error;
  use serde::de::{Deserialize, DeserializeOwned, Deserializer};
  
  #[derive(Serialize, Deserialize)]
  struct Example {
      #[serde(deserialize_with = "coercible")]
      first: f64,
      second: f64,
  }
  
  fn coercible&lt;'de, T, D&gt;(deserializer: D) -&gt; Result&lt;T, D::Error&gt;
  where
      T: DeserializeOwned,
      D: Deserializer&lt;'de&gt;,
  {
      use serde::de::Error;
      let j = String::deserialize(deserializer)?;
      serde_json::from_str(&amp;j).map_err(Error::custom)
  }
  
  fn typed_example() -&gt; Result&lt;(), Error&gt; {
      let data = r#"["3.141",1.618]"#;
      let e: Example = serde_json::from_str(data)?;
      println!("{} {}", e.first * 2.0, e.second * 2.0);
      Ok(())
  }
  
  fn main() {
      typed_example().unwrap();
  }
  </code></pre>
  
  <p>The above code compiles and runs as you would expect, outputting two floats.</p>
  
  <p>I'm trying to learn how the deserializer solution works, but I'd like to know if I'm headed in the right direction or if there is a better way to do this.</p>