Tutorials
Serialize Objects in JavaScript
Aug2809Aug 28, 09
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 (I believe this is implemented in JSON.stringify), I felt like it would be a chance to post my version 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 );
Style HR Horizontal Rule with CSS
Aug1809Aug 18, 09
First, the HTML for this tutorial is always the following:
- That's how the HTML specifications say we are to indicate a horizontal line, so that's all we want to use in the content layer. Presentation, on the other hand, will be a little more messy and unusual. First, let's remember the major browser quirks associated with the HR element, many of them from this article:
- Internet Explorer allows color to define the background color, and gives it presidence over background-color
- The border property does not behave correctly in Opera
- The height property is (by default) broken in Safari

Not that we should do everything Apple does, but they can whip up a mean UI.
To avoid this going on forever, here are the two bug fixes that need to be used to get things working across the major browsers:
Fixing the height property in Safari:
max-height: 0;
Making Internet Explorer 6 Behave:
However, it's always a good idea to predict when IE6 can be made to work with little extra effort. Typically, the problems people run into are with minimum heights due to the font-size property having precedence, and floats not breaking properly. To avoid these potential annoyances, I decided to include a few lines:
line-height: 0;
clear: both;
The rest is the same as you would have used if styling an empty div element instead of an hr element:
position: relative;
padding: 0;
margin: 8px auto;
height: 0;
max-height: 0;
width: 100%;
clear: both;
border: none;
border-top: 1px solid #AAA;
border-bottom: 1px solid #FFF;
font-size: 1px;
line-height: 0;
Now we've got a working horizontal rule, styled properly using only CSS.
It looks like this:
Beautiful, sleek, and it looks identical across all the browsers people use.


