title: How can I deserialize a type where all the fields are default values as a None instead?
body: <p>I have to deserialize JSON blobs where in some places the absence of an entire object is encoded as an object with the same structure but all of its fields set to default values (empty strings and zeroes).</p>
  
  <pre><code>extern crate serde_json; // 1.0.27
  #[macro_use] extern crate serde_derive; // 1.0.78
  extern crate serde; // 1.0.78
  
  #[derive(Debug, Deserialize)]
  struct Test&lt;T&gt; {
      text: T,
      number: i32,
  }
  
  #[derive(Debug, Deserialize)]
  struct Outer {
      test: Option&lt;Test&lt;String&gt;&gt;,
  }
  
  #[derive(Debug, Deserialize)]
  enum Foo { Bar, Baz }
  #[derive(Debug, Deserialize)]
  struct Outer2 {
      test: Option&lt;Test&lt;Foo&gt;&gt;,
  }
  
  fn main() {
      println!("{:?}", serde_json::from_str::&lt;Outer&gt;(r#"{ "test": { "text": "abc", "number": 42 } }"#).unwrap());
      // good: Outer { test: Some(Test { text: "abc", number: 42 }) }
  
      println!("{:?}", serde_json::from_str::&lt;Outer&gt;(r#"{ "test": null }"#).unwrap());
      // good: Outer { test: None }
  
      println!("{:?}", serde_json::from_str::&lt;Outer&gt;(r#"{ "test": { "text": "", "number": 0 } }"#).unwrap());
      // bad: Outer { test: Some(Test { text: "", number: 0 }) }
      // should be: Outer { test: None }
  
      println!("{:?}", serde_json::from_str::&lt;Outer2&gt;(r#"{ "test": { "text": "Bar", "number": 42 } }"#).unwrap());
      // good: Outer2 { test: Some(Test { text: Bar, number: 42 }) }
  
      println!("{:?}", serde_json::from_str::&lt;Outer2&gt;(r#"{ "test": { "text": "", "number": 0 } }"#).unwrap());
      // bad: error
      // should be: Outer { test: None }
  }
  </code></pre>
  
  <p>I would handle this after deserialization but as you can see this approach is not possible for enum values: no variant matches the empty string so the deserialization fails entirely.</p>
  
  <p>How can I teach this to serde?</p>