Click here to Skip to main content
15,886,689 members
Articles / Web Development / ASP.NET

JavaScript Serializer

Rate me:
Please Sign up or sign in to vote.
3.05/5 (16 votes)
6 Apr 2006CPOL2 min read 59.5K   284   30   6
A JavaScript generic serializer.

Introduction

Serialization is the most sensible way of exchanging information in any type of communication. Delivering data from the server and sending the response back is the most usual application of serialization.

Saving the object to a container and restoring the object from a container is a common and necessary operation in generic programming. When using the term container, I mean anything that can hold serialized objects (binary stream, XML stream, string etc.). When I started this project, I was surprised to find out that such a simple and useful thing was not implemented before in JavaScript in its pure form: saving an object and restoring the object without extra conditions imposed or external files involved.

Also, this operation must be self contained; otherwise, the whole exercise is pretty much pointless.

The serializer presented here can serialize complex JavaScript objects to a string and deserialize it from string in XML form. Why XML string? Because nothing else is available in JavaScript. Only strings.

JavaScript is a language with loose data typing which does not allow to get the type of the object directly. And, that is quite a problem when the object is to be restored from the stream. Fortunately, the object type (exactly not type, but name) can be retrieved from the constructor of the object and saved alone with the data. The restoring of the object is straightforward: get the type name of the object, create the instance, and fill the members recursively.

Using the code

JavaScript
var ContainerString;
var MyEmployee = new Employee();

MyEmployee.Name = "Tom";
MyEmployee.Address = new EmployeeAddress();
MyEmployee.Address.City = "HelloCity";
MyEmployee.Address.Street = "23 Wall";
MyEmployee.Title = "Mr";
MyEmployee.Position;
MyEmployee.Age = 99;
MyEmployee.Salary = 78588.868;
MyEmployee.PictureUrl = "http://fgh4545.com/a.jpg";
MyEmployee.Married = true;

function Button_serial_onclick() 
{
 ContainerString = JSerialize(MyEmployee);
 alert(ContainerString);  
}

function Button_des_onclick() 
{
 var NewObject = JDeserialize(ContainerString);
 alert(NewObject.Address.City + "," + MyEmployee.Name + 
                                "," + MyEmployee.Age);
}

function Employee()
{
 this.somelong;
 this.Salary;
 this.Scale;
 this.Name;
 this.SName;
 this.Title;
 this.Position;
 this.Age;
 this.Date;
 this.PictureUrl;
 this.Address;
 this.Married;
}

/* Employee inner class */
function EmployeeAddress()
{
 this.City;
 this.Country;
 this.Street;
 this.Phone;
}

Compatibility: IE and Mozilla (and Mozilla alike: Firefox ...)

Misconceptions about JSON and XML

There is common misconception that JSON is fat free XML. It is not. Strictly speaking, JSON is not an extensible XML like notation which is absolutely useless as a general purpose serializer. There are many reasons why not to use JSON. Those reasons are beyond the scope of this article. However there is just one reason that overweighs all others: JSON stream (in our case, string) does not contain the type of the object to be deserialized. It is impossible to deserialize the object on the server if the number of types is greater than one. Does that look like a serializer? To me, it does not. In other words, JSON is a purely academical construction with nicely drawn graphs and diagrams. But it has nothing to do with practical applications. The only place I see its value is a temporary storage on the web client. The inherent limitations do not allow to use it in the real world of generic programming. A discussion on the JSON thematic can be found here: http://www.codeproject.com/KB/scripting/jsonExample.aspx, at the bottom of the page.

Where to download the code

The full source code and sample can be found at http://dotnetremoting.com/ in the download section.

This serializer is used with the Rich Web Client SDK.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRich Web Client Framework Pin
Alex_117-Mar-08 23:49
Alex_117-Mar-08 23:49 
QuestionDWR Pin
Alexander Kosenkov12-Apr-06 12:55
Alexander Kosenkov12-Apr-06 12:55 
GeneralJSON Pin
Kevin Watkins6-Apr-06 5:14
Kevin Watkins6-Apr-06 5:14 
GeneralWauw! Pin
DeKale6-Apr-06 2:32
DeKale6-Apr-06 2:32 
GeneralRe: Wauw! Pin
Alex_17-Apr-06 0:24
Alex_17-Apr-06 0:24 
GeneralRe: Wauw! Pin
DeKale16-Apr-06 3:15
DeKale16-Apr-06 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.