Serialize Objects in JavaScript

Aug2809Aug 28, 09

Author's note: This is a simplification, and it is not recommended for use in public projects. The de-facto JSON library is written by Douglas Crockford, and is available at

I recently needed to store user preferences for a JavaScript application (on amoebaOS) in a file. Since JavaScript's support for XML is not amazing and XML in general is just an under-performing format for data storage (in my opinion), I opted for a JSON-encoded string as a means of storing the preferences. I know there are many libraries out there that might have this same functionality (JSON.stringify), I felt like it would be a chance to post my simplification of a JavaScript Object serialization method for the masses.

The goal was to have this code working perfectly without doing any intermediary testing, and that's what I did. The unserialize method is not elegant, but it's simpler than parsing manually and still safer than running a standard eval - you'll notice the dangerous global objects (or those that I could think of off the top of my head) are not available because the TinyJSON object declares internal variables that take precedence.

var TinyJSON = (function(){
	var window,top,self,parent,location,reload,back,forward,
	function addslashes(t){
		return t.replace(/(*)'/gim,"$1$1'");
	function strval(v) {
		switch(("").toLowerCase()) {
			case "number":
				return v+0;
			case "string":
				return "'" + addslashes(v).replace(/r?n/gim,'n') + "'";
			case "date":
				return "date("+v.getTime()+")";
			case "boolean":
				return v===true?"true":"false";
			case "array":
				var t = "[";
				for(var i=0; i<v.length; i++) {
					t += strval(v[i]) + ",";
				return t.replace(/,$/,'')+"]";
			case "object":
				return branch(v);
				return "null";
	function branch(obj) {
		var t = "{";
		for(var n in obj) {
			t += """ + n + "":" + strval(obj[n]) + ",";
		return t.replace(/,$/,'')+"}";
	var self = {
		serialize : function(obj) {
			return branch(obj);
		unserialize : function(str) {
			function date(s) {
				var d = new Date();
				return d;
			return eval('(' + str + ')');
	return self;

You can serialize a JavaScript Object to its string representation as follows:

var obj = {
	type : "myObj",
	data : [
		new Date()
	fun : true,
	going : 11,
	parent : {
		type : "parentObj"
var str = TinyJSON.serialize( obj );

You can convert a TinyJSON string back to object form as follows:

var str = "{"type":'myObj',"data":[123456,date(1251442681016)], 
var obj = TinyJSON.unserialize( str );

About Jason Miller:

I am a JavaScript developer from Waterloo, Ontario, Canada. When I am not typing green code onto a black screen, you might find me at the nearest coffee pub checking out the brew. I run a internet firm called developIT and maintain blogs and web apps when I can.
UGG Boots#

[url=][b]Longchamp Outlet[/b][/url]

[url=][b]NFL Jersey[/b][/url]

[url=][b]Yeezy Boost 350[/b][/url]

[url=][b]Jordan 4[/b][/url]

[url=][b]Nike Outlets[/b][/url]

[url=][b]Longchamp Bags[/b][/url]

[url=][b]Nike Air Max 2016[/b][/url]

[url=][b]Adidas UK[/b][/url]

[url=][b]Nike Outlet[/b][/url]

[url=][b]Under Armour[/b][/url]

[url=][b]Timberland UK[/b][/url]

[url=][b]Jordan 12[/b][/url]

[url=][b]Polo Ralph Lauren Outlet[/b][/url]


[url=][b]Nike Outlet[/b][/url]

[url=][b]Nike Huarache[/b][/url]

[url=][b]Keds Shoes For Women[/b][/url]

[url=][b]Roshe Run[/b][/url]

[url=][b]Yeezy Shoes[/b][/url]

[url=][b]Nike Air Max[/b][/url]

[url=][b]Ray Ban Outlet[/b][/url]

[url=][b]Oakley Outlet[/b][/url]

[url=][b]Adidas NMD[/b][/url]

[url=][b]Ray Ban Outlet Store[/b][/url]

[url=][b]Cheao Jordan Shoes[/b][/url]

[url=][b]Toms Outlet[/b][/url]

[url=][b]Ultra Boost Uncaged[/b][/url]

[url=][b]MLB Jerseys[/b][/url]

[url=][b]Ugg Boots Outlet[/b][/url]

[url=][b]Kate Spade Outlet[/b][/url]

[url=][b]Ugg Boots Outlet[/b][/url]

[url=][b]Uggs Outlet[/b][/url]

[url=][b]Timberland Outlet[/b][/url]

[url=][b]Ray Ban Outlet[/b][/url]

[url=][b]Ray Ban Sunglasses[/b][/url]

[url=][b]Adidas NMD[/b][/url]

[url=][b]Timberland UK[/b][/url]

[url=][b]Ralph Lauren[/b][/url]
Ann Tabares#
Michael Law#
Ann Tabares#
Shana Phillips#
Shana Phillips#
Kade Sapde Outlet# NFL Jerseys from China Yeezy Boost 350 V2 Longchamp Handbags Adidas Sale Ray Ban Outlet Yeezys Toms Shoes Outlet Online Kade Sapde Adidas Outlet Nike Store Under Armour Outlet Timberland Sale Jordan 12 Yeezy Shoes Nike Huarache Keds Shoes Yeezy Nike Air Max Toms Outlet Ultra Boost Uncaged MLB Jerseys Adidas NMD Oakley Outlet Ray Ban Kate Spade Outlet Adidas Outlet Nike Air Max 90 Timberland Outlet Ray Ban Sunglasses Adidas NMD Adidas EQT Ralph Lauren Jordan 4 Cheap Jordan Adidas NMD Kate Spade Longchamp
Leave a Comment

Post Comment