Serializing Objects as JSON using Atlas, JSON.NET and AjaxPro [Part 1]

Michael Schwarz on Wednesday, July 5, 2006

I read the post from Scott Hanselman [1] today comparing the internal use of JSON de-/serializer from the Atlas framework [2] and Ajax.NET Professional [3].

Scott writes that Ajax.NET Professional cannot serialize the Person object correct to JSON and back. This is wrong if you only use the serializer and deserializer. The JSON serializer in Ajax.NET Professional is working different, it will create a JSON string from any .NET data type. Some objects are serialized to a very special JSON output that is only used on client-side JavaScript code, i.e. the DataTable support will return the column description extracted from the table and a separate list of rows. This is done to minimize the data that is transferred to the client.

If you have a look at the DateTime example, if you get the JSON representation of this object you will get something like this:

new Date(Date.UTC(2006,6,5,9,36,9,250));/*

But, if you create a Date object on the client-side JavaScript code it will be serialized to following JSON representation:

{"__type":"System.DateTime","Year":2006,"Month":7,"Day":5,"Hour":9,
"Minute":26,"Second":9,"Millisecond":250,"TimezoneOffset":-120}

As you can see the JSON representation is very different, and with Atlas nothing is working more or better. If you have a deep look in Ajax.NET Professional you can see that currently a lot of data types are supported with special JSON representation to save nearly 50% traffic.

Conclusion is: never compare different things that are not comparable. Ajax.NET Professional allows you to create a Person object on the client-side code with the correct type on the server-side, this is not working in all AJAX frameworks I currently know.

The __type property is used to allow to use the object in ArrayLists or any other object list. The next thing is that if you are using different types that are inherited from a common interface you'd like to get the correct type instead of only getting the properties defined in the common abstract class. This is not working with Atlas...!! See an great example for the Person class at my example page [4] where I will use result.save() to save the returned Person class instance.

At my last talk about Ajax.NET Professional the first speaker (Oliver Scheer, Microsoft) talked about Atlas. In the discussion after his talk there where a couple of developers that had exactly this problem with Atlas. With Ajax.NET Professional it is more working than using .NET on the client-side.

Updated: see my part 2 of this article here [5].