I have an interest into going into detail over the serialization framework provided by ASP.NET AJAX. The reason - ultimately - is that I would like to start explaining how the Animation framework works in ASP.NET AJAX. However, as with any sort of understanding, there are earlier pieces that must be grasped first before you can truly understand the later pieces: the "crawl before you walk" problem. So, let's start off with the basics and, over the next couple days, we'll move our way through the serialization framework and into the Animation framework of ASP.NET AJAX. Doing so, you'll be able to really start cooking with some fascinating animations in your browser-side programming. My goal, also, is to aid you in understanding how it all works under the hood (as usual).
So, let's explore JSON.
JSON - the JavaScript Object Notation - is a "stringified" version of an object; or an object represented as human-readable text. It is valuable because the web, primarily, doesn't do well with raw, binary data....as you know, the data must be encoded so that it can be properly transmitted via the web medium (learn more). However, it is more space-savvy then various alternatives (such as XML), thereby making it more appealing to transmit large, detailed blocks of data (such as objects).
The basic idea behind JSON is that when you need to transmit some object over the web boundary, you serialize it in the JSON wire-format, make the transmission, and then deserialize it on the receiving end into the original object. The result is a clean hand-off of the object from one universe to another across the finicky web. The actual data format isn't important; though if you are interested Wikipedia has a nice overview of it here. But, suffice it to say that in JSON, you're taking some opaque object and representing it as a block of ASCII-based text.
Why is it important?
In ASP.NET AJAX, especially, JSON is VERY important. Why? Because ASP.NET AJAX is striving, very hard, to be as strongly-typed as it can at all times for objects written in ECMA script. Objects are flittering and flying about all over the ASP.NET AJAX world and, therefore, a consistent wire-format for transmitting and working with those objects is necessary. As we will see when we start encountering the Animation framework of ASP.NET AJAX, serialization via JSON is used extensively in converting the animation XML into maleable, server-side objects that we may program against; likewise, those same objects on the server can be serialized into meaningful JavaScript objects for the client-side framework. All of this is done using JSON formatting in ASP.NET AJAX.
How does it work?
I mentioned earlier that the formatting rules of JSON isn't important - and for us, as ASP.NET AJAX programmers - it isn't. The reason is due to the fact that we rarely ever need to hand-tweak anything because formatters for JSON exist and are quite mature already, on both the client AND the server-sides. However, there is a powerful framework at work here that deserves a bit of understanding, regardless of the actual formatting rules.
At the core, the most important element required for JSON serialization within the browser itself is the eval() method. The eval() method takes a block of data, which can be either script or JSON data, and evaluates it. The "evaluation" process will do something to the text; if it's script, then it will execute it, otherwise it will go through and parse out the JSON data and return a deseralized object back to the script. There are security implications involved in this sort of process, and so you should be very cautious to leverage eval() on your own. Instead, you should use Sys.Serialization.JavaScriptSerializer as an ASP.NET AJAX developer. This type exposes two, obvious methods: serialize() and deserialize(). Under the hood, this JavaScriptSerializer object leverages eval(), but more cautiously.
So, JSON works by leveraging API frameworks that convert the formatted data to objects and back again for us.
Conclusion
In the next part of this topic I'll go over the actual process of taking objects represented in XML and creating ECMA script from them (and back again); a process that is used extensively in the Animation framework of ASP.NET AJAX to work with the animation objects declaratively.