Error While Serailize/De-Serailize DateTime in JSON format : A Solution

Sometimes back, I was working on one my modules in my application. Here I had an of a Class which has some properties of DateTime type. Actually I was serializing the object using JavaScriptSerializer to serialize it in JSON format but later could not be able to deserialize it again.

So I thought of digging deep to this problem. So  I will be discussing it with an example. Let’s have a class

public class Employee
{
    public int UId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public DateTime DateofBirth { get; set; }
    public DateTime DateofJoining { get; set; }
}

Now Here the functionality is like,  User can search to any employee if it has the Id. Or s/he can create a new employee using the form. And here, I am not using full post-back or Update Panel. I am using AJAX (jQuery) to call a web service to save a new employee or getting an employee from Database.

So lets move to first scenario, creating a new employee. So Here I have a object something like this

Person p =new Person { UId = 1, Name = "Brij", Address = "Noida",
DateofBirth = new DateTime(1983,03,02) , DateofJoining = DateTime.Now}

So when we serialize this object using JavaScriptSerializer. we will get the date in this format..

/Date(1307888377953)/

So, it can not be de-serialized into DateTime using JSON.  So what is this digits enclosed in braces

Actually the number is in millisecond format of the date.  It is he count of milliseconds from 1st Jan 1970. But obviously you cannot display in this format. One need to change the this into the Date format.

So to change this, first one need to remove the unnecessry keys from the string. Then try to de-serialize it. So to convert this, one need to remove ‘Date(‘ and ‘)/‘ with empty string, like

p.DateofJoining.replace(“/Date(“, “”).replace(“)/”, “”)

Now the string that you’ll get, is the time is milliseconds. Now if you want to convert it in the date format. It can be done as

new Date(milliseconds)

One constructor of Date in java script accepts the milliseconds and this can be used.

The dilemma is if you have a object and you serialize it using the JavaScriptSerializer of .net and you try to de-serialize at the same time, you’ll get the error. So there is some issue in serialization/de-serializtion in JavaScriptSerializer .

Now lets move to another scenario, User enters the data in the form and enters the date. Now you create the object at Client side and before sending it to the server you use the stringify mrthod of JSON library. Then you date object will be intact and it will be serialized like “02/03/1983” .  And this can be easily converted in a server side using JavaScriptSerializer and at client side also. So in this scenario you wont face any problem.

Hope this will help you to overcome this problem.

Enjoy coding!!

Brij

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s