Tagged as

JSON Serialization and Deserialization in ASP.NET

This article focuses on JSON Serialization and Deserialization in ASP.NET.

JSON is one kind of data format which is designer for running JavaScript on websites. At present, JSON is widely used in web. This article focuses on JSON Serialization and Deserialization in ASP.NET, including the brief introduction of JSON, how to serialize and deserialize with ASP.NET and operation on date, assembly, dictionary.

String: Any quantity unicode character assembly which is enclosed with quotation marks. It uses backslash to escape.

2. Serialize and Deserialize JSON Data

We can use DataContractJsonSerializer to serialize type instance to JSON string and deserialize JSON string to type instance. DataContractJsonSerializer is under System.Runtime.Serialization.Json namespace. It is included in System.ServiceModel.Web.dll in .NET Framework 3.5 and System.Runtime.Serialization in .NET Framework 4.0. We need to add it as reference.

Reserialization Demo

Result

In ASP.NET, JSON serializaation and deserialization can use JavaScriptSerializer which is under System.Web.Script.Serialization namespace. We need to add System.Web.Extensions.dll as reference or use JSON.NET.

3. JSON Serialization and Deserialization on DateTime

JSON cannot support date and time directly. The value of DateTime is shown as "/Date(700000+0500)/". The first number (700000) stands for milliseconds from Jan. 1, 1970 according to base time (not saylight saving time) in GMT. The number can be negative to present time before Jan. 1, 1970. The part "+0500" is optional. It present the time is Local, in other words, it can be converted to local time zone when deserializing. If this part is not there, the time will be deserialized as UTC.

Comments and Discussions

When I run my project it gives me this error:
The type 'System.Xml.XmlReader' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
but I have referenced System.Xml.
This error comes from the first T from this syntax:T t = (T)ser.ReadObject(ms);

From Section 3: JSON Serialization and Deserialization on DateTime, regarding the optional part like "+0500", sometimes I get a negative value for the operator like "-0500", which will not be handled by your regular expression pattern:

@"\\/Date\((\d+)\+\d+\)\\/"

I would like to confirm what that optional part is. Is it the time zone? And when do I get a positive or a negative operator for that part? I'm always getting a positive operator on my local machine, and a sometimes a negative operator on our cloud server. That's why I guessed it is the time zone.

The optional part in the serialized value is the timezone offset from UTC. The offset represents the number of hours and minutes (+HHMM) that get added to UTC to get the representation of the local time. In my timezone, American Eastern Standard Time (EST), the offset is "-0500" during winter and "-0400" during summer because Daylight Saving Time applies. EST is 5 hours behind UTC, EDT is 4 hours behind.

Put another way, if the offset is present, it indicates the value represents local time and gives you enough information to calculate it. The mandatory part is number of ticks since midnight of January 1, 1970 UTC.

Without the offset, the serialized value represents Universal aka UTC time (also called Zulu time).

Just remember that Javascript always treats a date-time value as if it were in the browser's local time zone.

And you are correct, the regex pattern should be: @"\\/Date\((\d+)[-+]\d+\)\\/"--Ed Eaglehouse

The problem with this, is our Android and iPhone apps are generating the code like how the JavaScriptSerializer does, our WCF service is de-serializing the request using DataContractJsonSerializer and failing saying bad format.

How can i make the WCF to use JavaScriptSerializer to de-serialize the hashmap that is sent in the request?