title: Can I serialize nested properties to my class in one operation with Json.net?
body: <p>Lets say I have a model like:</p>
  
  <pre><code>public class MyModel
  {
      public string Name { get; set; }
      public string[] Size { get; set; }
      public string Weight { get; set; }
  
  }
  </code></pre>
  
  <p>And Json like this:</p>
  
  <pre><code>{
      "name" : "widget",
      "details" : {
          "size" : [
              "XL","M","S",
          ]
          "weight" : "heavy"
      }
  }
  </code></pre>
  
  <p>I have been trying to work out a way to serialize my object without making one model for the "name" and one model for the "details" as this doesn't map nicely to my database so involves a little juggling to get class populated.</p>
  
  <p>I can make multiple passes at JsonConvert.PopulateObject() like:</p>
  
  <pre><code>var mod = new MyModel();
  
  JsonConvert.PopulateObject(json.ToString(), mod);
  JsonConvert.PopulateObject(json["details"].ToString(), mod);
  </code></pre>
  
  <p>But in my real code I am running multiple threads and PopulateObject is not thread safe, it jams the app.  The comments for PopulateJsonAsync() say not to use it but instead to use Task.Run() on PopulateObject().</p>
  
  <p>This does not work and still locks the app when I call it like:</p>
  
  <pre><code>await Task.Run(() =&gt; JsonConvert.PopulateObject(response.ToString(), productDetail));
  
  if (response["results"].HasValues)
  {
      await Task.Run(() =&gt; JsonConvert.PopulateObject(response["results"][0].ToString(), productDetail));
  }
  </code></pre>
  
  <p>A few get through but eventually the app gets completely thread locked.  If I remove PopulateObject the threads all terminate fine so I am pretty sure this function is not thread safe.</p>
  
  <p>Is there a neat threadsafe approach to populating my object in a single step?   </p>