What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7734 |  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: 32668 | 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

I am going to explain about javascript code for Drag from ASP.Net ListBox and to Drop in the specific Cursor location of TextArea .

Like most programming tasks, client validation can be as simple or as more complex .In this article I am going to explain the Client Side Texbox Number/Characters Validation with JavaScript .

In this article we will look as how to use String.Format function of C# in JavaScript and display multiple values of a resource file.

Many times we need a way to write object oriented code in javascript. We can create classes,objects,properties and lot more using javascript. This article include some of the features about object oriented scripting which i found very useful during developement.

In this article I am going to show how to get value from asp.net controls like TextBox, RadioButtonList, DropDownList, CheckBoxes through JavaScript.

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 find 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/23/2013 6:18:17 AM