Skip to content

Commit

Permalink
added serialization functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed Sep 30, 2011
1 parent 78d5acd commit e17879c
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/porthole.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,57 @@ Porthole.WindowProxy.splitMessageParameters = function(message) {
return hash;
};

/**
* Serialize an object to a query string.
*
* @param {Object} obj The object to be serialized
* @return {String} Serialization
*/
Porthole.WindowProxy.serialize = function(obj, prefix) {
var key, new_key, new_value, result = [];
for(key in obj) {
if (obj.hasOwnProperty(key)) {
new_key = prefix ? prefix + '.' + key : key;
new_value = obj[key];
result.push(typeof new_value == 'object' ?
Porthole.WindowProxy.serialize(new_value, new_key) :
encodeURIComponent(new_key) + '=' + encodeURIComponent(new_value));
}
}
return result.join('&');
};

/**
* Unserialize a query string to an object, where names can use dotted notation.
*
* @param {String} text Serialization
* @return {Object} name-value pairs
*/
Porthole.WindowProxy.unserialize = function(text) {
var result = {};
var decoded_nameValue;
var pairs = text.split('&');

var dotted = function(obj, dotted_key, val) {
obj = obj || {};
dotted_key = dotted_key.split('.');
var key = dotted_key.shift();
obj[key] = dotted_key.length ? dotted(obj[key], dotted_key.join('.'), val) : val;
return obj;
};

for (var keyValuePairIndex in pairs) {
if (pairs.hasOwnProperty(keyValuePairIndex)) {
var nameValue = pairs[keyValuePairIndex].split('=');

nameValue[1] = nameValue[1] || '';
result = dotted(result, nameValue[0], nameValue[1]);
}
}

return result;
};

/**
* Event object to be passed to registered event handlers
* @param {String} data
Expand Down

0 comments on commit e17879c

Please sign in to comment.