title: How can I deserialize an ADO.NET DataTable that contains null values using Json.NET?
body: <p>I am attempting to use <a href="http://json.codeplex.com/" rel="nofollow">Newtonsoft.Json.Net35</a> Version 4.0.2.0 to deserialize an ADO.NET DataTable that contains null values.  Serialization works fine:</p>
  
  <pre><code>    [Test]
      public void SerializeDataTableWithNull()
      {
          var table = new DataTable();
          table.Columns.Add("item");
          table.Columns.Add("price", typeof(double));
          table.Rows.Add("shirt", 49.99);
          table.Rows.Add("pants", 54.99);
          table.Rows.Add("shoes"); // no price
  
          var json = JsonConvert.SerializeObject(table);
          Assert.AreEqual(@"["
              + @"{""item"":""shirt"",""price"":49.99},"
              + @"{""item"":""pants"",""price"":54.99},"
              + @"{""item"":""shoes"",""price"":null}]", json);
      }
  </code></pre>
  
  <p>Deserialization works fine if values are missing:</p>
  
  <pre><code>    [Test]
      public void DerializeDataTableWithImplicitNull()
      {
          const string json = @"["
              + @"{""item"":""shirt"",""price"":49.99},"
              + @"{""item"":""pants"",""price"":54.99},"
              + @"{""item"":""shoes""}]";
          var table = JsonConvert.DeserializeObject&lt;DataTable&gt;(json);
          Assert.AreEqual("shirt", table.Rows[0]["item"]);
          Assert.AreEqual("pants", table.Rows[1]["item"]);
          Assert.AreEqual("shoes", table.Rows[2]["item"]);
          Assert.AreEqual(49.99, (double)table.Rows[0]["price"], 0.01);
          Assert.AreEqual(54.99, (double)table.Rows[1]["price"], 0.01);
          Assert.IsInstanceOf(typeof(System.DBNull), table.Rows[2]["price"]);
      }
  </code></pre>
  
  <p>If, however, values are explicitly null:</p>
  
  <pre><code>    [Test]
      public void DerializeDataTableWithExplicitNull()
      {
          const string json = @"["
              + @"{""item"":""shirt"",""price"":49.99},"
              + @"{""item"":""pants"",""price"":54.99},"
              + @"{""item"":""shoes"",""price"":null}]";
          var table = JsonConvert.DeserializeObject&lt;DataTable&gt;(json);
          Assert.AreEqual("shirt", table.Rows[0]["item"]);
          Assert.AreEqual("pants", table.Rows[1]["item"]);
          Assert.AreEqual("shoes", table.Rows[2]["item"]);
          Assert.AreEqual(49.99, (double)table.Rows[0]["price"], 0.01);
          Assert.AreEqual(54.99, (double)table.Rows[1]["price"], 0.01);
          Assert.IsInstanceOf(typeof(System.DBNull), table.Rows[2]["price"]);
      }
  </code></pre>
  
  <p>DeserializeObject throws "System.ArgumentException : Cannot set Column 'price' to be null. Please use DBNull instead."  The following workaround works for my particular JSON:</p>
  
  <pre><code>        var regex = new Regex(@",?""[_\w]+"":null");
          var nullless = regex.Replace(json, string.Empty);
          var table = JsonConvert.DeserializeObject&lt;DataTable&gt;(nullless);
  </code></pre>
  
  <p>but like all regular expression-based kludges this is clearly brittle.</p>
  
  <p>Finally, the questions:</p>
  
  <ol>
  <li>Is this a bug?</li>
  <li>Json.NET has many events that can be hooked.  Is there a way to get notified when a when a null value is encountered and explicitly set the value to System.DBNull?</li>
  </ol>
  
  <p>Thanks in advance,</p>
  
  <p>Frank</p>