ICONICO

The Workshop

Javascript Serializer

Javascript Serializer
The Workshop If you enjoy technical Javascript then you'll love this project. The Javascript Serializer allows you to take any javascript object, including some really complex ones, and serialize it out to a string.

The fun part is what types of strings you can create, you can create a Javascript Object string. What's the use of that, you might ask? Well imagine you created a shopping cart javascript object to store products on the client. Why you'd want to do that is unclear, but just say you had your reasons. When people add objects to the cart, stored as a complex javascript object, you could serialize the whole thing and store the string a cookie. Then on every page load you could check if the cookie was there and reserialize it as a javascript object simply by calling eval() on it. Then when you're ready to post it back to the server you can serialize it as an XML string and post that file. I was going to write an example of this but you'll have to make do with the demo below.

The javascript is divided into three separate files. Serializer.js does the hard work of converting the javascript objects into an internal format. You can then plug in SerializerJS.js or Serializer.XML which adds in the GetJSString() and GetXMLString() methods automatically. It's split up like this as you could add in differnet formatters later. The obvious one to write would be GetHTMLString() but I haven't done that.

This code is inspired by JSON, but it would be a mistake to think of it as the same thing. JSON makes no note of an object's type, and the Javascript Serializer keeps the exact type of an object, even noting if a string was created literally or using the 'String' constructor. There are some obvious applications for interplay with Ajax here that hopefully someone will fully explore, and an XML to Javascript object converter is also on the list of things to do.

I've used the Serializer on this site in a few places. The EasyRead login URLs are created using it, and the CSS Scrollbar values are stored to a cookie using it.

If you want to play around with the files then you can download the zip files, and if you build a good demo then please send us a copy. The files should work on all modern browsers.

Don't try and serialize the serializer object, it'll just make your head hurt.
Live Demo
Preferences
SmartIndent
ShowLineBreaks
ShowTypes

Types
UseNull
UseUndefined
UseArray
UseObject
UseBoolean
UseDate
UseError
UseFunction
UseNumber
UseRegExp
UseString
UseUserDefined
UseObjectsForUserDefined

Rules
CheckInfiniteLoops
 MaxDepth


This page contains a Javascript object called objMonkey.
We put objMonkey in the JSSerializer and then call GetJSString():
Then we call GetXMLString():

Our Software Stores

IconicoAccurate Design and Development Software

BitsDuJourDiscount Deal Coupons for Windows and Mac Software Apps

Our Software Services

SoftwareMarketingResourceYou Wrote the Code, Now How do you Sell it?

IcoBlogOur Official Blog

© copyright 2004-2014 Iconico, Inc. Code & Design. All Rights Reserved. Terms & Conditions Privacy Policy Terms of Use