Thursday, October 18, 2007

JSON - a Better Alternative to XML?

JSON is picking up favour among programmers as the format for data interchange. Here are some of my notes, taken as I read about it in this MSDN article:

JSON syntax is very terse and yields formatted text where most of the space is consumed by the represented data. No additional application code required to parse text; can use JavaScript's eval function.

When using JSON as the data exchange format, two common tasks are turning a native and in-memory representation into its JSON text representation and vice versa. Unfortunately, at the time of writing, JavaScript does not provide built-in functions to create JSON text from a given object or array. Until these JSON formatting functions are formally added to JavaScript and widely available across popular implementations, use the reference implementation script available for download at http://www.json.org/json.js.
In its latest iteration at the time of this writing, the json.js script at www.json.org adds toJSONString() functions to array, string, Boolean, object, and other JavaScript types. The net result of the toJSONString() functions is that any type can be converted into its JSON format with a single function call.

Parsing JSON text is even simpler. Since JSON is merely a subset of JavaScript literals, it can be parsed into an in-memory representation using the eval(expr) function, treating the source JSON text as JavaScript source code. The eval function accepts as input a string of valid JavaScript code and evaluates the expression.

The eval function blindly evaluates whatever expression it is passed. An untrustworthy source could therefore include potentially dangerous JavaScript along with or mixed into the literal notation that makes up the JSON data. In scenarios where the source cannot be trusted, it is highly recommended that you parse the JSON text using the parseJSON() function (found in json.js).

When working with JSON for .NET, you can use the ASP.NET AJAX library, or you can use Jayrock, an open-source implementation for JSON for the .NET framework.
Working with JSON in .NET using Jayrock is similar to working with XML through the XmlWriter,XmlReader and XmlSerializer classes in the .NET Framework. The classes JsonWriter, JsonReader, JsonTextWriter, and JsonTextReader found in Jayrock mimic the semantics of the .NET Framework classes XmlWriter, XmlReader, XmlTextWriter, and XmlTextReader. These classes are useful for interfacing with JSON at a low- and stream-oriented level. Using these classes, JSON text can be created or parsed piecemeal through a series of method calls. For example, using the JsonWriter class method WriteNumber(number) writes out the appropriate string representation of number according to the JSON standard. The JsonConvert class offers Export and Import methods for converting between .NET types and JSON. These methods provide a similar functionality as found in the XmlSerializer class methods Serialize and Deserialize, respectively.

Note: One of the sore points of JSON is the lack of a date/time literal. Most applications using JSON as a data format, therefore, generally tend to use either a string or a number to express date and time values.

0 comments: