Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3398 |  Welcome, Guest!   Register  Login
Home > Articles > JavaScript > Serialize Object in JavaScript

Serialize Object in JavaScript

1 vote(s)
Rating: 5 out of 5
Article posted by Mehulthakkar1 on 1/7/2010 | Views: 21637 | Category: JavaScript | Level: Beginner red flag


This Article will help you getting the serialized object from javascript.

Introduction

Sometimes while developing application you would have to Serialize/De-serialize the object. With this article once can serialize the object in JavaScript, where the object is created in JavaScript only. But why to Serialize object in JavaScript. The answer is, some times we want to pass the object to web service from JavaScript, it is not possible to perform this. So you can serialize it and send it.

How to serialize an Object

Every JavaScript object/variable has it's own type defined in constructor. You can check that property and identify the type.

var obj = new Array();
alert(obj.constructor);

This will alert "Array" in dialog.

Now lets write the method to serialize the object.

function serialize(obj)
{
  var returnVal;
  if(obj != undefined){
  switch(obj.constructor)
  {
   case Array:
    var vArr="[";
    for(var i=0;i<obj.length;i++)
    {
     if(i>0) vArr += ",";
     vArr += serialize(obj[i]);
    }
    vArr += "]"
    return vArr;
   case String:
    returnVal = escape("'" + obj + "'");
    return returnVal;
   case Number:
    returnVal = isFinite(obj) ? obj.toString() : null;
    return returnVal;    
   case Date:
    returnVal = "#" + obj + "#";
    return returnVal;  
   default:
    if(typeof obj == "object"){
     var vobj=[];
     for(attr in obj)
     {
      if(typeof obj[attr] != "function")
      {
       vobj.push('"' + attr + '":' + serialize(obj[attr]));
      }
     }
      if(vobj.length >0)
       return "{" + vobj.join(",") + "}";
      else
       return "{}";
    }  
    else
    {
     return obj.toString();
    }
  }
  }
  return null;
}

Check this "serialize" function, it will accept the parameter of any type and return you serialize object.

var x = new Object();
x.Name = "xyz"
;
x.Phone = 222222;
x.Email = "mehul@xyz.com"
;
var y = serialize(x);
alert(y);

This will alert {"Name":'xyz', "Phone":222222, "Email":'mehul@xyz.com'} string in dialog. After serialize it one can send it to server side. The created x object can not be passed directly to the server without serialize it. User can pass any type of varible/object in this method and get the serialize object.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Mehulthakkar1
Experience:7 year(s)
Home page:
Member since:Thursday, October 08, 2009
Level:Starter
Status: [Member]
Biography:System Analyst
Mehul Thakkar
>> Write Response - Respond to this post and get points
Related Posts

This article explains the use of execCommand method used in Javascript that is used to execute a command in JavaScript.

I am going to explain the simple GridView Client Side Validation instead of server side validation controls.

Prototype is used to define classes or objects in JavaScript. Classes or objects are building blocks of Object Oriented Programming.

While writing UI code, we must have came across scenaio where we need to get the asp:RadioButtonList value into JavaScript and validate some other field.

For many reasons sometime we don't want to allow user to use right click to copy paste or by using ctrl+C , ctrl+v to copy and paste in textbox on a aspx page in asp.net

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:36:02 AM