Json ISO Date not deserialized as DateTime object
I am creating a multiplatform application that works on .net/mono and uses Newtonsoft.Json to serialize information.
All worked well until I tried to send information between the machines with exactly the same version of Newtonsoft.Json.dll.
What turned out it that dates were properly serialized in the ISO format like that: "2013-02-08T12:11:39Z" , but when I try to deserialize them on Mac OS I get a string back instead of DateTime object!
I did some experiments using the serializer settings:
private static readonly JsonSerializerSettings jsonSettings = new JsonSerializerSettings {
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = DateParseHandling.DateTime,
Converters = new JsonConverter [] { new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ" } }
};
JsonConvert.DeserializeObject<object> (""2013-02-08T10:11:51Z"", jsonSettings);
But the result is till exactly the same string "2013-02-08T10:11:51Z" . I did some more experiments and it turned out that using exactly the same settings I am not able to serialize and deserialize the date like that:
JsonConvert.DeserializeObject<object>(JsonConvert.SerializeObject(DateTime.Now, jsonSettings), jsonSettings);
I still get a string representing the ISO date. After that I tried this too:
JsonConvert.DeserializeObject<object>(JsonConvert.SerializeObject(DateTime.Now));
And now I get the proper DateTime object as a result. But running the following code shows that the way the date is serialized is not exactly the ISO format:
JsonConvert.SerializeObject(DateTime.Now);
Returns something like ""2013-02-08T12:22:25.974177+02:00"" which is not standart and will not be able to work for communication.
The weirdest part is that I had a client wit a Mac that had no such problems. I am starting to think that this is somehow related to the OS, but then wouldn't the exact format specified by me fix that?
链接地址: http://www.djcxy.com/p/8138.html