title: JSON Deserialization Despair (unable to deserialize nested types)
body: <p>There are probably 500 questions like this on SO, and a million websites out there all offering tidbits of information - but I just can't see the wood for the trees. This seems like it should be embarrassingly simple to do, but I just can't make it work.</p>
  
  <p>I have a WCF webservice that returns a serialized JSON object:</p>
  
  <pre><code>[OperationContract(Name = "PeopleData"), WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "people/{subset}", ResponseFormat = WebMessageFormat.Json)]
  PeopleObject GetPeople(string subset);
  </code></pre>
  
  <p>This works - if I hit that URI from a browser, GetPeople is invoked and returns a JSON-serialized <code>PeopleObject</code> (actual data values redacted for privacy here):</p>
  
  <pre><code>{"HashValue":"XXXXX","People":[{"EmailAddress":"XXXXX","EmployeeID":99999,"Gender":"X","JobTitle":"XXXXX","Office":"","PreferredName":"XXXXX","Surname":"XXXXX","WorkExtensionNumber":"XXXXX","WorkPhoneNumber":"XXXXX","Department":"XXXXX","DeskNumber":"XXXXX","EmploymentClassification":"XXXXX","InternationalExtensionNumber":"XXXXX","IsFirstAider":false,"Languages":[{"LanguageID":9,"LanguageSkillID":9},{"LanguageID":9,"LanguageSkillID":9}],"QualificationInitials":"XXXXX","QualificationTitle":"XXXXX","Secretaries":null,"WorkMobilePhoneNumber":"XXXXX"}],"RecordCount":"1","SizeBytes":"12345"}
  </code></pre>
  
  <p>In this example the <code>PeopleObject</code> payload contains just one <code>Person</code> object in the collection, but could contain many (depending on the parameter supplied in <code>/{subset}</code>.</p>
  
  <p>Here is the class hierarchy for <code>PeopleObject</code> - it's a top-level container holding some metadata about the payload, and a List&lt;> of <code>Person</code> objects. Those objects in turn have a bunch of simple type attributes, plus two further nested List&lt;> of <code>Language</code> and <code>Secretary</code> objects (which may or may not be populated):</p>
  
  <pre><code>  [DataContract]
    public class PeopleObject
    {
      [DataMember]
      public string HashValue { get; set; }
      [DataMember]
      public List&lt;Person&gt; People { get; set; }
      [DataMember]
      public string RecordCount { get; set; }
      [DataMember]
      public string SizeBytes { get; set; }
    }
  
    [DataContract]
    public class Person
    {
      [DataMember]
      public string EmailAddress { get; set; }
      // &lt;-- snip - lots of fields like this, no point listing them all here
      [DataMember]
      public bool IsFirstAider { get; set; }
      [DataMember]
      public List&lt;Language&gt; Languages { get; set; }
      [DataMember]
      public List&lt;Secretary&gt; Secretaries { get; set; }
    }
  
    [DataContract]
    public class Language
    {
      [DataMember]
      public int LanguageID { get; set; }
      [DataMember]
      public int LanguageSkillID { get; set; }
    }
  
    [DataContract]
    public class Secretary
    {
      [DataMember]
      public int EmployeeID { get; set; }
      [DataMember]
      public char FirstSurnameLetter { get; set; }
    }
  </code></pre>
  
  <p>So far, so good - WCF responds with a JSON structure that contains all the fields and their contents. Now to deserialize that structure in a client application (using the same class hierarchy definitions):</p>
  
  <pre><code>  // I have a little helper-class to manage the WCF request and return a Stream
    using (Stream response = wcfHelper.GetRequestResponseStream(MY_WCF_URI))
    {
      // This is debug code to prove the response arrives as expected - it does
      //StreamReader sr = new StreamReader(response);
      //Console.WriteLine("\nResponse:\n{0}", sr.ReadToEnd());
  
      // Deserialise the response
      DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(PeopleObject));
      PeopleObject p = (PeopleObject)dc.ReadObject(response);
  
      // The object shows 1 record (in the example) but nothing in the List&lt;&gt;
      Console.WriteLine("\nDeserialized records: '{0}' [{1}]", p.RecordCount, p.People.Count);
    }
  </code></pre>
  
  <p>So this correctly deserializes the container object, giving me the record count, hash value, and payload size in bytes. The object does also have a List&lt;> of Person objects, but it's null - the content from the JSON response hasn't successfully rehydrated the List&lt;> by creating and adding a Person object.</p>
  
  <p>What am I missing? My understanding was that this rehydration of the C# object hierarchy from the JSON structure should happen automatically, so either that's not the case (and I need to write some code to make it happen) or it is, but I've missed something obvious.</p>