Wed 14 May 2008
Invalid JSON from NET – DateTime
Posted by NZEYIMANA Emery Fabrice under C# , English , Languages , PHP , Software1 Comment
With my need to exchange data between a ASP.NET and PHP web applications, I decided to use JSON. The .NET team did a good job by integrating JSON de/serialization into the language (NET 3.5) but they decided not to follow JSON specifications for some good reasons. Serializing an object that has a DateTime property will insert a string that won’t be understood by json_decode of PHP.
On the site of JSON, there is no such thing as a date type. I have taken the ISO 8601 path which is my preferred date format (MySQL and Swedish Locale standard)
Consider the following class:
[code lang="C#"]
[DataContract]
public class Person
{
[DataMember]
public DateTime DateOfBirth { set; get; }
[DataMember]
public string Names { set; get; }
}
[/code]
By Serializing it you will get something like
[code]
{"DateOfBirth" : "\/Date(1210408872000+0200)\/", "Names" : "Kavuna ka Lyaziga"}
[/code]
That Date is not defined as a JSON type. In case your JSON will be used directly by JavaScript or .NET (C#, VB) you will not need to write extra codes.
September 9th, 2008 at 20:11
Actually this is inaccurate: this is perfectly valid JSON. Kind of the whole point of it actually: an unambiguous convention for dates that is perfectly compatible with the JSON spec.