title: deserializing json object with nested lists
body: <p>I have an object that contains nested lists and a method to deserialize it from json using custom converters and .net's javascript serializer. Something like this:</p>
  
  <pre><code>public class MyObject{
        int TheID { get; set; }
        public List&lt;MyNestedObject1&gt; ListOfMyNestedObject1 { get; set; }  
        public List&lt;MyNestedObject2&gt; ListOfMyNestedObject2 { get; set; }
  
        public MyObject ObjectFromJson(string TheObjectInJson) {
  
          JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
          TheSerializer.RegisterConverters(new JavaScriptConverter[] {
              new MyObjectConvert()
          });
          TheSerializer.RegisterConverters(new JavaScriptConverter[] {
              new MyNestedObject1Convert()
          });
          TheSerializer.RegisterConverters(new JavaScriptConverter[] {
              new MyNestedObject2Convert()
          });
          //if I comment out the registrations of the converters, it works
          //but I need the converters of the nested objects to kick in
          return TheSerializer.Deserialize&lt;MyObject&gt;(TheObjectInJson);       
      }
  }
  </code></pre>
  
  <p>The json converters for the nested objects both look like this:</p>
  
  <pre><code>public class MyNestedObject1Convert : JavaScriptConverter {
  
    public override IEnumerable&lt;Type&gt; SupportedTypes {
        get { return new Type[] { typeof(MyNestedObject1Convert) };
    }
    public override object Deserialize(IDictionary&lt;string, object&gt; dictionary, Type type, JavaScriptSerializer serializer) 
    { //deserialization logic
      return MyNestedObject1;}
  }
  </code></pre>
  
  <p>And the converter for MyObject looks like this:</p>
  
  <pre><code>public class MyObjectConvert : JavaScriptConverter {
  
    public override IEnumerable&lt;Type&gt; SupportedTypes { get { return new Type[] { typeof(MyObjectConvert) }; }  
  
    public override object Deserialize(IDictionary&lt;string, object&gt; dictionary, Type type, JavaScriptSerializer serializer) {
  
      int TheID;
      MyObject TheObject = new MyObject();
  
      int.TryParse(serializer.ConvertToType&lt;string&gt;(dictionary["TheID"]), out TheID))
      TheObject.ID = TheID;
  
      return TheObject;
      }
  }
  </code></pre>
  
  <p>Now the calling function that receives the json string and looks to return the c# object looks like this: </p>
  
  <pre><code>MyObject AMyObject = new MyObject();
  MyObject TheMyObject = new MyObject();
  
  TheMyObject = AMyObject.ObjectFromJson(JsonString);
  </code></pre>
  
  <p>When I run this code, the returned object contains TheID but the nested objects are null. I'm registering the converters in the object method but I'm guessing that's not the way to do it. If I remove the registration of the converters, the object DOES contain the nested objects, but then the converters don't kick in. </p>
  
  <p>What do I need to change? Note: I'm not looking to use another library, just to make the native deserializer work.</p>
  
  <p>Thanks for your suggestions.</p>