title: Is it possible to deserialize a "ISODate" field of MongoDB to a JToken (C#) ?
body: <p><strong>Scope:</strong></p>
  <p>I am writing a <a href="https://github.com/MarcelloLins/MongoTools/" rel="noreferrer">set of tools</a> to help people run common operations on their MongoDB databases, and &quot;Exporting&quot; data is one of them.</p>
  <p>Currently I support full JSON export and &quot;CSV&quot;, but the latter is way trickier.</p>
  <p>The exporting tool allows for a &quot;ConfigFile&quot; which specifies which fields will be deserialized (from a <a href="http://docs.mongodb.org/v2.2/core/document/" rel="noreferrer">BsonDocument</a>), not caring about their type. Most of types are currently working, but &quot;ISO&quot; Dates are still giving me headaches.</p>
  <p><strong>Dynamic Deserialization</strong></p>
  <p>Currently I am relying on <a href="http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm" rel="noreferrer">JObjects</a> to handle the Parsing of the &quot;Json&quot; documents, just like this :</p>
  <pre><code>        // Json Writer Settings - To avoid problems with 10Gen types
          var jsonSettings = new JsonWriterSettings () { OutputMode = JsonOutputMode.Strict };
  
          // Mapping string to a dynamic json object
          JObject mappedJson = JObject.Parse (jsonObject.ToJson (jsonSettings));
          
          // Trying to extract property values out of the object
          foreach (Field field in _configuration.Fields)
          {
                  // Checking for JToken Type
                  JTokenType objType = fieldData.Type;
                  
                  // Sanity Check for NULL Values of properties that do exist
                  if (objType == JTokenType.Null)
                  {
                      fieldValue = String.Empty;
                  }
                  else if (objType == JTokenType.Array) // Checking for Arrays (that need to be serialized differently)
                  {
                      String[] valuesArray = fieldData.Select (t =&gt; t.Value&lt;String&gt; ().Replace (_configuration.ListDelimiter, String.Empty)
                                                                                      .Replace (_configuration.Delimiter, String.Empty)).ToArray ();
  
                      fieldValue = String.Join (_configuration.ListDelimiter, valuesArray);
                  }
                  else if (objType == JTokenType.Object &amp;&amp; field.Name.Equals (&quot;_id&quot;)) // Checking for specific MongoDB &quot;_id&quot; situation
                  {
                      fieldValue = fieldData.ToObject&lt;String&gt; (); // Value&lt;ObjectId&gt; ().ToString ();
                  }
                  else
                  {
                      // Reaching Attribute Value as &quot;String&quot; (if nothing else worked)
                      fieldValue = fieldData.Value&lt;String&gt; ();
                  }
          }
  </code></pre>
  <p><strong>The Problem:</strong></p>
  <p>This code works for all types I have tested so far, but &quot;DateTime&quot;. The way MongoDB stores is as follows : <code>&quot;PublicationDate&quot; : ISODate(&quot;2014-08-10T00:00:00.000Z&quot;)</code>, which completely breaks my deserialization.</p>
  <p>I have tried to deserialize it as &quot;DateTime&quot; and as &quot;Object&quot;, but both of them fails to work. Is there any proper way of doing this ? This is basically all I am missing to make this &quot;Dynamic Exporter&quot; works.</p>
  <p>Thanks in advance</p>