"Sven Neuberg" <do********@nospampls.net> skrev i meddelandet
news:86********************@twister.southeast.rr.c om...
"Joakim Braun" <jo**********@jfbraun.removethis.com> wrote in message
news:zk****************@nntpserver.swip.net...
For instance, say you collect form values into an array like:
var theThing = new Array("johnsmith", "Yes", 1232);
You could convert this into something like:
Array{key sz:1{0} string sz:9{johnsmith} key sz:1{1} string sz:3{Yes}
key sz:1{2} int sz:4{1232}}
You post it to the server, which reconstitutes the array. This would
work regardless of the input values and it's a bit easier to maintain.
Thank you! I am going to look into this. It looks like it could be very
helpful.
Below is my own code which works for my needs. It turns arrays (which may
contain other arrays as well as strings, booleans and numbers) into strings
and back. Array keys are preserved. There are probably better/more
efficient/more compact ways to do this. Note that strings are escaped,
you'll have to unescape them server-side.
Joakim Braun
***
function serializeArray(inArray){
var theResult = "";
var arrayConstructor = new Array().constructor.toString();
var thing = true;
for(var i in inArray){
var theType = typeof(inArray[i]);
var key = escape(i.toString());
theResult += "key sz:" + key.length + "{" + key + "}";
if(inArray[i] && inArray[i].constructor &&
inArray[i].constructor.toString() == arrayConstructor){
var arrData = serializeArray(inArray[i]);
theResult += "Array sz:" + arrData.length + "{" + arrData + "}";
}
else{
var str = escape(inArray[i].toString());
theResult += theType + " sz:" + str.length + "{" + str + "}";
}
}
return theResult;
}
function unserializeArray(inString){
var theResult = new Array();
var theKey = "";
// Note that arrays with zero entries will come in as "Array sz:0{}".
// The recursion will then be called with "zero string",
// which is why we check for inString.length
while(true && inString.length > 0){
var nextLeftBracketIndex = inString.indexOf("{", 0);
var theType = inString.substr(0, inString.indexOf(" ", 0));
var sizeOffset = inString.indexOf("sz:", 0) + 3;
var dataSize = parseInt(inString.substr(sizeOffset, nextLeftBracketIndex -
sizeOffset));
var theData = inString.substr(nextLeftBracketIndex + 1, dataSize);
// This will save legacy arrays
if(theKey == "")
theKey = theResult.length;
if(theType == "key"){
if(!isNaN(theData)){
theKey = parseInt(theData);
}
else if(theData.length > 0){
theKey = unescape(theData);
}
}
else if(theType == "number"){
theResult[theKey] = parseFloat(theData);
theKey = "";
}
else if(theType == "string"){
theResult[theKey] = unescape(theData);
theKey = "";
}
else if(theType == "boolean" ){
var val = new Boolean(theData);
theResult[theKey] = val == true ? true : false;
theKey = "";
}
else if(theType == "Array"){
// Recurse
theResult[theKey] = unserializeArray(theData);
theKey = "";
}
else{
prompt("Unknown object: " + theType + "\ninString: " + inString,
inString);
}
nextLeftBracketIndex = nextLeftBracketIndex + 2 + dataSize;
if(inString.length - nextLeftBracketIndex > 0){
inString = inString.substr(nextLeftBracketIndex, inString.length -
nextLeftBracketIndex);
}
else
break;
}
return theResult;
}