This project has moved. For the latest updates, please go here.


Exception while deserializing DateTime in version 5


We used Json.NET 4 to serialize Dictionary<DateTime, string> and save it to database.

Now we updated NuGet package to the version 5 and the software throws an exception:
// from database
var myjson = "{\"03/27/2013 02:51:38\":\"customer note\"}";

JsonConvert.DeserializeObject<Dictionary<DateTime, string>>(myjson);

Throws exception:

Could not convert string '03/27/2013 02:51:38' to dictionary key type 'System.DateTime'. Create a TypeConverter to convert from the string to the key type object. Path '03/27/2013 02:51:38', line 1, position 23.
Closed Apr 30, 2013 at 9:26 AM by JamesNK
I have fixed it so that Json.NET will deserialize the old format dates.


denyss86 wrote Apr 28, 2013 at 10:17 PM

Here is console application to reproduce this bug:
namespace ConsoleApplication3
    class Program
        static void Main(string[] args)
            var dic = new Dictionary<DateTime, string>();

            var time1 = DateTime.UtcNow.Date;

            dic.Add(time1, "test");

            var json = JsonConvert.SerializeObject(dic);


First install the lastest version of Json.NET and execute this code.

Then install the previous version of Json.NET with the following statement:
Install-Package -Id "Newtonsoft.Json" -Version "4.5.11"
If you compare both versions, you see that they serialize DateTime different way:
Version 5.0.4:

Version 4.5.11
{"04/28/2013 00:00:00":"test"}
If a dictionary was serialized with an older version of Json.NET, the new version cannot deserialize it and throws an exception.

wrote Apr 30, 2013 at 9:26 AM

wrote May 16, 2013 at 12:36 PM