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 json.org/json2.js.

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,
		alert,confirm,prompt,stop,frames,document;
	
	function addslashes(t){
		return t.replace(/(*)'/gim,"$1$1'");
	}
	function strval(v) {
		switch((v.constructor.name+"").toLowerCase()) {
			case "number":
				return v+0;
			break;
			case "string":
				return "'" + addslashes(v).replace(/r?n/gim,'n') + "'";
			break;
			case "date":
				return "date("+v.getTime()+")";
			break;
			case "boolean":
				return v===true?"true":"false";
			break;
			case "array":
				var t = "[";
				for(var i=0; i<v.length; i++) {
					t += strval(v[i]) + ",";
				}
				return t.replace(/,$/,'')+"]";
			break;
			case "object":
				return branch(v);
			break;
			default:
				return "null";
			break;
		}
	}
	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();
				d.setTime(s+0);
				return d;
			}
			return eval('(' + str + ')');
		}
	};
	
	return self;
})();

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

var obj = {
	type : "myObj",
	data : [
		123456,
		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)], 
	"fun":true,"going":11,"parent":{"type":'parentObj'}}";
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.
Comments
cheap jordans#
http://www.topsneakerwholesale.com/
http://www.adidasultraboost.uk/
http://www.adidasyeezy.us.org/
http://www.cheapsnapbackhats.us.com/
http://www.cheapyeezys.fr/
http://www.yeezysboost350.fr/
http://www.adidasyeezy.uk/
http://www.soronshop.com/
http://www.cheapairmax2018.co.uk/
http://www.dearlover.us/
http://www.kickstmall.com/
http://www.adidasultraboost.fr/
http://www.adidasyeezy.fr/
http://www.adidasyeezy350boost.uk/
http://www.adidasyeezy350boost.us.org/
http://www.cheapyeezy350boost.us/
http://www.yeezy350boost.uk.com/
http://www.yeezyboost350.fr/
http://www.yeezyboost350.com.de/
http://www.yeezyboost350forsale.co.uk/

http://www.yeezyboost350forsale.us.org/
http://www.yeezyboost.me.uk/
http://www.yeezyshoes.it/
http://www.yeezyshoes.me.uk/
http://www.airjordansforsale.us.com/
http://www.cheapjordan12.us.com/
http://www.cheapjordans.org/
http://www.cheapjordansforsale.co.uk/
http://www.cheapjordanshoes.org/
http://www.cheapjordansmen.us.com/
http://www.cheapjordansonline.us.org/
http://www.cheapjordansshoes.us.org/
http://www.cheappumashoes.us.com/
http://www.cheaprealyeezys.us.com/
http://www.cheapunderarmourshoes.com/
http://www.cheapyeezyboost350v2.us.com/
http://www.cheapyeezyv2.com/
http://www.jordansforcheap.us.org/
http://www.jordanshoescheap.us.org/
http://www.cheapairjordans.us.org/

http://www.cheapadidasshoes.us.com/
http://www.cheapcurryshoes.us.com/
http://www.cheapfakeyeezys.com/
http://www.cheapjordan11.us.com/
http://www.cheapjordansforsale.us.org/
http://www.cheapjordanshoes.us.org/
http://www.cheapkobes.us.com/
http://www.cheaplebronshoes.us.com/
http://www.cheapnikeshoes.us.org/
http://www.cheapnmd.com/
http://www.cheaprealjordans.us.org/
http://www.cheapyeezys.us.org/
http://www.cheapyeezyshoes.us.com/
http://www.jordanscheap.us.org/
http://www.lebron12cheap.com/
http://www.cheapadidasnmd.us.org/
http://www.cheapairforceones.us.com/
http://www.cheapairmax90.us.org/
http://www.cheapjordanshoesformen.us.com/
http://www.cheapjordansonline.co.uk/

http://www.cheapkdshoes.us.com/
http://www.cheapkobeshoes.us.com/
http://www.cheaplebron13.us.com/
http://www.cheaplebrons.us.com/
http://www.cheapnikeairmaxshoes.us.com/
http://www.cheapnikeroshe.us.com/
http://www.cheapretrojordans.us.org/
http://www.cheapultraboost.us.com/
http://www.cheapunderarmour.us.com/
http://www.cheapyeezyboost350.com/
http://www.cheapyeezys350.com/
http://www.cheapyeezysforsale.us.com/
http://www.discountjordans.us.com/
http://www.jordansforcheap.co.uk/
http://www.nikerosheruncheap.com/
http://www.offwhitecheap.com/
http://www.wholesalenikeshoes.us.com/

http://www.cheapchristianlouboutin.co.uk/
http://www.cheapchristianlouboutin.us.com/
http://www.christianlouboutinshoessale.co.uk/
http://www.christianlouboutinshoessale.us.org/
chanyuan#
http://www.nikelunarglide.us
http://www.poloralph.us.com
http://www.toryburchoff.us.com
http://www.belstaffoutlet.us.com
http://www.cheapraybans.ca
http://www.christianlouboutinshoessale.org.uk
http://www.birkenstockshoes.org.uk
http://www.truereligion-jeans.us
http://www.michael--kors.me.uk
http://www.salvatoreferragamo.in.net
http://www.michael-korstaschen.ch
http://www.juicycoutureoutlet.us.com
http://www.basketballshoesstore.us.com
http://www.vibram-fivefingers.us.com
http://www.fingerlingsmonkey.org
http://www.ralphlauren.in.net
http://www.adidasnmdforsale.us.com
http://www.adidasjeremyscott.in.net
http://www.nikekyrie3.us.com
http://www.outlet-celine.com
http://www.y3shoes.us.com
http://www.ralph-laurenpolouk.org.uk
http://www.burberry-outlet-stores.us.com
http://www.huarache.us.com
http://www.ray-banssunglasses.org.uk
http://www.louisvuitonnoutlet.com
http://www.tory-burch.org.uk
http://www.guccislides.us.com
http://www.pradaoutletonlinestore.us.com
http://www.newbalance-shoes.us.com
http://www.nikecortez.net
http://www.supremeparis.fr
http://www.katespadeoutletsale.us.com
http://www.ralph-laurens.co.uk
http://www.kobe12shoes.us.com
http://www.audemarspiguet.us.com
http://www.converse.me.uk
http://www.valentino.us.org
http://www.nikeoff.com
http://www.rayban-sunglassesonsale.us.com
http://www.ralphlaurenoutletofficial.us.com
http://www.adidasoutletadidas.us.com
http://www.michaelkorsoutlet70off.us.com
http://www.cheapmlbjerseysshop.us.com
http://www.converseshoes.us.org
http://www.redbottomsshoes.me.uk
http://www.hermesbags.me.uk
http://www.jordan1.us
http://www.cheapnfljerseysnike.us.com
http://www.pradahandbagsoutlet.us.com
http://www.raybansunglassesforwomen.us.com
http://www.alexandermcqueen.us.com
http://www.christianlouboutinoutletus.us.com
http://www.pandorajewelrysoutlet.us.com
http://www.jordan5.us
http://www.adidaswings.name
http://www.louisvuittonofficial.us.com
http://www.nikeairmaxuk.co.uk
http://www.rayban.net.co
http://www.oakley-sunglassesonsale.us.com
http://www.balenciaga-triples.us.com
http://www.korsmichaelkors.org.uk
http://www.katespade-outlet.in.net
http://www.michaelkorss--outlet.us.com
http://www.cheapbestnbajerseys.us.com
http://www.louis--vuittonhandbags.us.com
http://www.airmax95.us.org
http://www.soccerjersey.us.com
http://www.timberlandoutlet.us.org
http://www.coach-factoryonline.us.org
http://www.nike-airhuarache.me.uk
http://www.kobe11.in.net
http://www.cheapreplicawatches.us.com
http://www.jimmychoooutlet.us.com
http://www.lightup-shoes.us.com
http://www.coachoutlet-factory.us.com
http://www.swarovskijewelry.co.uk
http://www.oakleysunglassescanada.ca
http://www.coachfactoryoutlet-onlinestore.us.com
http://www.louboutins.us.com
http://www.oakleysunglassesparts.us.com
http://www.cheapnfljerseysstore.us.com
http://www.fitflopsoutlet.us.com
http://www.coachfactoryoutlet-online.eu.com
http://www.nikeairforce1.net
http://www.bcbg.us.com
http://www.undefeated.us.com
http://www.pumaslides.us.com
http://www.airforce1.in.net
http://www.cartierjewelry.us.com
http://www.airhuarache.fr
http://www.taghuer.us.com
http://www.toryburchbags.us.com
http://www.fila.us.com
http://www.baseballjersey.us.com
http://www.pandorajewelrycanada.ca
http://www.suicoke.us.com
http://www.stuartweitzmanshoes.us.com
http://www.supreme-newyork.com
http://www.rayban.in.net
http://www.longchampoutlet-online.com
http://www.coachoutlet70off.us.com
http://www.runningshoes.us.org
http://www.linksoflondonuk.me.uk
http://www.suprashoesclearance.us.com
http://www.pandoracharmjewelry.us.com
http://www.michael-kors-uk.me.uk
http://www.malonesouliers.us.com
http://www.raybansunglassesoutlet.net.co
http://www.mizuno.us.org
http://www.jordans-fusion.com
http://www.tomsoutlet.in.net
http://www.mbtscarpe.it
http://www.nikeblazer.fr
http://www.louisvuittonoutletbuy.us.com
http://www.nikeairmax.me.uk
http://www.nikejordans.co.uk
http://www.balenciaga.us.org
http://www.sunglassesoakleycheap.us.com
http://www.montblancpen.in.net
http://www.airjordan11.us
http://www.supremeclothing.us.com
http://www.swarovskijewelryoutlet.us.com
http://www.louisvuittonusa.us.com
http://www.newbalance.me.uk
http://www.cheapraybansunglasses.net.co
http://www.newcoachoutlet.us.com
http://www.manoloblahnik.us
http://www.hardenvol2.us.com
http://www.nhlhockey-jerseys.us.com
http://www.dolceandgabbana.us.com
http://www.truereligionoutletjeans.us
http://www.adidasflipflops.us.com
http://www.cheapnhljerseysshop.us.com
http://www.fitflopssaleclearance.us.org
http://www.burberryoutletonline.us.org
http://www.airmax2017.us.com
http://www.ferragamooutlet.us.com
http://www.nikeairmaxschweiz.ch
http://www.coachsoutletstoreonline.us.com
http://www.rolexwatchess.us.com
http://www.balenciagasandals.us.com
http://www.rolexwatches.us.com
http://www.ralph-lauren.com.co
http://www.montblancfountainpens.us.com
http://www.rolexwatch.us.com
http://www.poloralphlaurenpascher.fr
http://www.toryburchoutletsaleonline.us.com
http://www.nikerevolution.us
http://www.michael-korsoutletstores.us.org
http://www.rolexwatch.me.uk
http://www.ray-bansunglassesoutlet.com.co
http://www.adidasstoreuk.me.uk
http://www.cheap-jordans.us.com
http://www.montblanc.com.co
http://www.coachfactory--outlet.us.com
http://www.ralplaurenuk.org.uk
http://www.jerseys.us.org
http://www.michaelkorshandbagsmacys.us.com
http://www.michaelkorsoutlet.us
http://www.adidasslides.us.com
http://www.airjordan-14.us
http://www.louis-vuittonoutletus.us.com
http://www.nikeukstores.me.uk
http://www.givenchyhandbags.us.com
http://www.fitflopsandals.org
http://www.marcjacobs.me.uk
http://www.bestnfljerseyswholesale.us.com
http://www.louis-vuitton-borse.it
http://www.coachonline.us.org
http://www.nikeoutletus.us.com
http://www.niketn.fr
http://www.jordans.us.org
http://www.asics.us.org
http://www.flops.us.com
http://www.adidasoriginalsstansmith.us.com
http://www.breitling.us.com
http://www.nikeshoesnike.us.com
http://www.cheapmichaelkorshandbags.us.org
http://www.niketennis.us.com
http://www.adidasoutletstores.net
http://www.kevindurantshoes.us.com
http://www.hollisteroutlet.us.org
http://www.vansshoesoutlet.us.com
http://www.jordans.in.net
http://www.coachoutlet-online.eu.com
http://www.jordan12.org
http://www.coachhandbagsfactoryoutletonline.us.com
http://www.converseshoesoutlet.us.com
http://www.louisvuittonparis.fr
http://www.ralphlauren-poloshirts.co.uk
http://www.airmaxplus.org
http://www.prada.us.org
http://www.coachwallets.us.com
http://www.poloralphlaurenoutlet.org.uk
http://www.nfljerseyshop.us.com
http://www.newbalancesandals.us.com
http://www.burberrysale.us.org
http://www.stussy.us.com
http://www.nikesshoes.us.com
http://www.louisvuittonstores.us.com
http://www.nikepresto.us.com
http://www.viphandbags.us
http://www.pandorajewelry-canada.ca
http://www.maccosmeticsofficial.us.com
http://www.shoecarnival.us.com
http://www.christianlouboutinoutletuk.org.uk
http://www.ralph-lauren.fr
http://www.katespadehandbagsoutlet.us.org
http://www.airmax2018.in.net
http://www.nikeepicreactflyknit.us.com
http://www.airmax270.org
http://www.authenticlouisvuittonhandbags.us.com
http://www.jordanshoes.us.org
http://www.polooutletinc.us.com
http://www.nikefreeruns.us.com
http://www.mcmbackpack.us.com
http://www.oakleysunglassescom.us.com
http://www.longchampoutletcom.us.com
http://www.tods.us.com
http://www.nikeroshe.us.com
http://www.beatsbydrdresolo.us
http://www.underarmouroutletclearance.com
http://www.birkenstockoutletonline.us.com
http://www.mulberryukhandbags.org.uk
http://www.pandora-jewelry.name
http://www.longchampoutlet-store.us.com
http://www.louboutinchristian.us.com
http://www.pradabags.us.com
http://www.hermesbeltoutlet.com
http://www.gucci-outlet.cc
http://www.coachoutletonlinestore.ca
http://www.adidasultra-boost.us.com
http://www.air-jordan.us.com
http://www.nikeairmax90.us.org
http://www.coach-bags.us.org
http://www.hermeskellybag.us.com
http://www.off-whiteclothing.us.com
http://www.nikefree.net
http://www.coachoutletonline-sale.us.com
http://www.michaelkorsoutletoff.us.com
http://www.bottega.in.net
http://www.coachoutletonsale.us.com
http://www.adidasshoess.us.com
http://www.supremeuk.co.uk
http://www.coachcom.us.com
http://www.truereligion-jeans.me.uk
http://www.vancleefarpelsjewelry.us.com
http://www.timberlandoutletuk.org.uk
http://www.longchamphandbagstotes.us.com
http://www.birkenstocksandalsonsale.us.com
http://www.hermesoutlets.us.com
http://www.michaelkorsoutletcheapest.us.com
http://www.oksunglasshut.com
http://www.yeezydesertrat500.us.com
http://www.nikefree-run.fr
http://www.stansmithadidas.org.uk
http://www.wholesaleoakleysunglasses.us.org
http://www.adidasoutletstores.com
http://www.poloshirtsoutlet.us.com
http://www.baseballbats.us.com
http://www.columbia-sportswear.us.com
http://www.manchesterunitedjersey.us.com
http://www.nikesb.in.net
http://www.burberryscarves.us.com
http://www.authenticmlbjerseyscheap.us.com
http://www.michaelkorsoff.us.com
http://www.cheapnfljerseys.us.org
http://www.outletmichaelkorsoutletonline.us.com
http://www.cheapjerseys.in.net
http://www.airjordan.me.uk
http://www.christianlouboutinoff.us
http://www.blackhawksjersey.us.com
http://www.louisvuitton-handbags.com.co
http://www.kanyeyeezyshoes.us.com
http://www.beatsheadphones.us.com
http://www.louis-vuittonoutlets.us.org
http://www.nikefree.me.uk
http://www.michaelkorsonline.ca
http://www.mishka.us.com
http://www.snapbacks-wholesale.us.com
http://www.cheap-oakleysunglasses.com
http://www.hermesbirkinbags.us.com
http://www.weddingshoes.us.com
http://www.tomsoutletonline.us.com
http://www.superdry.me.uk
http://www.mbt.us.com
http://www.hardenvol1.us.com
http://www.fitflopssale.org.uk
http://www.louboutins.me.uk
http://www.redbottomshoes.org.uk
http://www.ralphlaurenpolo.in.net
http://www.pandorajewelry.us.org
http://www.vipbestbags.com
http://www.coachoutletstoreonlineofficial.us.com
http://www.nikeairmax90.com.de
http://www.airjordanretro.fr
http://www.coachoutletfactoryofficial.us.com
http://www.coachoutletonline70off.us.com
http://www.cheapraybansunglass.co.uk
http://www.nikefactoryoutletstores.us.com
http://www.off-white.us.com
http://www.michaelkorsoutletcheapest.us.com
http://www.katespadestoreoutlet.us.com
http://www.timberlandoutletonline.us.com
http://www.brequet.us.com
http://www.airmax.us.org
http://www.airmaxnl.nl
http://www.chaussuresnikeairmax.fr
http://www.oakleysunglasshut.us.com
http://www.outletralph-lauren.us.com
http://www.nikeflipflops.org
http://www.hervelegerdresses.us.com
http://www.louisvuittonlvoutlet.us.com
http://www.timberland.us.org
http://www.pandoraukcharms.co.uk
http://www.oakleysunglasses-vault.us.com
http://www.louboutinoutlets.us.com
http://www.truereligionjeans-outlet.us.com
http://www.barcelonajersey.us.com
http://www.dansko.us.com
http://www.airmax97.org
http://www.poloralphoutlet.us.com
http://www.miumiu.us.com
http://www.cheapjordansshoes.in.net
http://www.asics.org.uk
http://www.redbottoms.us.org
http://www.truereligionsale.com.co
http://www.ferragamoshoesoutlet.us.com
http://www.cheapmlbjersey.us.com
http://www.vansshoes.us.org
http://www.adidasfootballboots.org.uk
http://www.longchamp-outletstore.us.com
http://www.burberryoutlet-canada.ca
http://www.jordanxx9.in.net
http://www.louis-vuittonoutlet.net.co
http://www.katespadehandbagssale.us.org
http://www.jordanretro.us
http://www.air-jordanpascher.fr
http://www.michaelkorsshandbags.us.com
http://www.coachoutletstore.eu.com
http://www.nikeshox.in.net
http://www.nikedunks.us
http://www.louisvuittonoutletmalls.us.com
http://www.louissvuitton.us.com
http://www.timberlandpro.us.com
http://www.raybansunglassescheapest.us.com
http://www.coachoutlet-handbags.us.com
http://www.nikemercurial.us
http://www.rayglasses.us
http://www.adidasnmdoutlet.us.com
http://www.christian-louboutins.fr
http://www.lebron15.in.net
http://www.pumafentysandals.us.com
http://www.katespade-outlet.us.com
http://www.adidassuperstarshoes.us.com
http://www.nikeblazers.us
http://www.airmax1.us
http://www.fitflops.us.org
http://www.stephen-curryshoes.us.com
http://www.adidasyeezy350boost.us.com
http://www.niketrainers.org.uk
http://www.airmaxnike.me.uk
http://www.christiandiorhandbags.us.com
http://www.nikeoutletstoreonline.us.com
http://www.longchampsacsoldes.fr
http://www.giuseppe-zanotti.us.com
http://www.flipflops.me.uk
http://www.bvlgari.us.com
20185.18wengdongdong
Leave a Comment

Post Comment