title: C# automatic property deserialization of JSON
body: <p>I need to deserialize some JavaScript object represented in JSON to an appropriate C# class. Given the nice features of automatic properties, I would prefer having them in these classes as opposed to just having fields. Unfortunately, the .NET serialization engine (at least, by default) totally ignores automatic properties on deserialization and only cares about the backing field, which is obviously not present in the JavaScript object.</p>
  
  <p>Given that there's <a href="https://stackoverflow.com/questions/371398/problems-with-auto-properties">no standard way to name backing fields</a> and to be honest I don't even want to bother with the "let's create a JavaScript object that looks like it had C# backing fields" approach as it sounds a bit dirty, the only way I could serialize JavaScript fields to C# auto-properties if I could force the serialization engine to somehow ignore the backing field and use the property directly. Unfortunately, I can't figure out how this is done or if this can be done at all. Any ideas would be appreciated.</p>
  
  <p><strong>EDIT</strong>: Here's an example:</p>
  
  <p>Javascript:</p>
  
  <pre><code>function Cat()
  {
      this.Name = "Whiskers";
      this.Breed = "Tabby";
  }
  var cat = new Cat();
  </code></pre>
  
  <p>This is then serialized to "{Name: 'Whiskers'}".</p>
  
  <p>The C# class:</p>
  
  <pre><code>[Serializable()]
  public class Cat
  {
      public string Name { get; set; }
      public string Breed { get; set; }
  }
  </code></pre>
  
  <p>And the deserialization code, that fails:</p>
  
  <pre><code>new DataContractJsonSerializer(typeof(Cat)).ReadObject(inputStream);
  </code></pre>
  
  <p>And it is apparent from the exception that it fails because it is looking for the backing field.</p>
  
  <p><strong>EDIT2</strong>: Here's the exception, if that helps (no inner exceptions):</p>
  
  <blockquote>
    <p>System.Runtime.Serialization.SerializationException</p>
    
    <p>"The data contract type 'Test.Cat'
    cannot be deserialized because the
    required data members
    '<code>&lt;Name&gt;k__BackingField, &lt;Breed&gt;k__BackingField</code>' were not
    found."</p>
  </blockquote>