Use Javascript objects like you do it in C#

Michael Schwarz on Friday, July 16, 2004
Labels

While developing websites (intranet applications) I had to save a lot of information on the client or for a user. First, I used cookies to save information. I noticed that I can only save up to 20 cookies with a lenght of about 192 chars I searched for a new way of saving cookies. While I am only developing intranet applications for the Internet Explorer, I decided to use a webservice to save cookies.

[WebMethod(Description="")]

public bool SetCookieEx(string AuthenticationTicket, string Name, string Value) { MS.Security.Authentication auth = new MS.Security.Authentication(AuthenticationTicket); int UserID = auth.UserID; int DataSetID = auth.DataSetID; return ControlSettings.SaveSettingsObject(UserID, DataSetID, Name, Value); } [WebMethod(Description="")]

public string GetCookieEx(string AuthenticationTicket, string Name) { MS.Security.Authentication auth = new MS.Security.Authentication(AuthenticationTicket); int UserID = auth.UserID; int DataSetID = auth.DataSetID; return ControlSettings.GetSettingsObject(UserID, DataSetID, Name); } Now, I took a look at my information to save. It would be nice to have objects in Javascript, allow to save these objects to the database for each user, and retreive a object again. I wrote a simple serializer/deserializer that will take my Javascript object, convert this object to a xml stream and save it as a string.

Here is my Javascript code (note: the script is not published complete here, contact me if you want to have my current version):

function

Serializer() { this._xml = new ActiveXObject("Microsoft.XMLDOM"); this._xml.loadXML("<Serializer/>"); this.serialize = Serializer_serialize; this.deserialize = Serializer_deserializeXml; } function

Serializer_deserialize(node) { var o = new Object(); for(var i=0; i<node.childNodes.length; i++) { if(node.childNodes[i].tagName "string") { return node.childNodes[i].text; } else if(node.childNodes[i].tagName "number") { return node.childNodes[i].text; } else if(node.childNodes[i].tagName "boolean") { return node.childNodes[i].text "true"; } else if(node.childNodes[i].tagName == "dateTime") { var d = new Date(parseInt(node.childNodes[i].text)); d.setHours(d.getHours() + d.getTimezoneOffset() / 60); return d; } // else all other objects

return o; }

function

Serializer_deserializeXml(xml) { // deserialize code ... } function

Serializer_serialize(obj, node, root) { if(node null && root null) { node = this._xml.documentElement; root = this._xml; } if(obj null) { var ele = root.createNode("element", "null", ""); node.appendChild(ele); } else if(typeof(obj) "string") { var ele = root.createNode("element", "string", ""); ele.text = obj.valueOf(); node.appendChild(ele); } else if(typeof(obj) == "number") { var ele = root.createNode("element", "number", ""); ele.text = obj.valueOf(); node.appendChild(ele); } // else all other objects

return root.xml;

}

Now, you can use the code to save Javascript objects using ASP.NET (WebService) and a Database

<script>

var o = new Object(); o.OrderNumber = "615161"; o.CustomerName = "SIEMENS"; o.Quantity = 4000; o.IsProduced = false;

var s = new Serializer();

SetCookieEx("currentorder", s.serialize(o));

// to get the object back from the webservice

var oo = GetCookieEx("currentorder");

if(oo.IsProduced) alert(oo.CustomerName + " " + oo.OrderNumber)

</script>

Currently we are developing a second serializer (is already working with all Microsoft's WSDLs generated by .NET) to use the same objects as you use in C# or Visual Basic.NET.

public class MyClass { public string FirstName = "Michael"; public string LastName = "Schwarz"; public int Age = 0; public DateTime Birthday = DateTime.MinValue; }

[WebMethod(Description="")] public MyClass GetMyObject() { return new MyClass();    // ok, very simple!   ;) }

Now, in Javascript the code will look like this:

<script>

var s = new Serializer();

var o = GetMyObject();

alert(o.FirstName + " " + o.LastName); alert("Age = " + o.Age + "\r\nBirth: " + o.Birthday.toLocaleString());

</script>

When finished the second serializer for WSDL standard classes I will post the code here!