It would be impossible to serialize it the way you are hoping to.
With this object:
function request(url) {
this.url = url;
this.head = [];
}
This variation:
var r = new request("http://test.com");
r.head.push({"cookie": "version=1; skin=new"});
r.head.push({"agent": "Browser 1.0"});
document.write(JSON.stringify(r));
would give you:
{"url":"http://test.com","head":[{"cookie":"version=1; skin=new"},{"agent":"Browser 1.0"}]}
If you were to change the object to:
function request(url) {
this.url = url;
this.head = {};
}
var r = new request("http://test.com");
r.head["cookie"] = "version=1; skin=new";
r.head["agent"] = "Browser 1.0";
document.write(JSON.stringify(r));
would give you:
{"url":"http://test.com","head":{"cookie":"version=1; skin=new","agent":"Browser 1.0"}}
The first variation is guaranteed to give you the head values in order when you iterate it. It also has the advantage in that you can later insert things in specific order if that is of interest.
The second version, by convention, will give you the items back in the order they were inserted as long as there are no number based keys, but that ordering is not guaranteed by the ecma spec.