Skip to content

Instantly share code, notes, and snippets.

@javierhonduco
Created December 14, 2012 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierhonduco/4281425 to your computer and use it in GitHub Desktop.
Save javierhonduco/4281425 to your computer and use it in GitHub Desktop.
Url serialization helpers in javascript
(function () {
uri = {}; var response = "?";
uri.serialize = function(_params){
for(var _value in _params){
response += encodeURIComponent(_value);
response += "=";
response += encodeURIComponent(_params[_value]);
response += "&";
}
return response;
};
uri.deserialize = function(_serialized){
var result = _serialized.split("?")[1].split("&"), object = {},iterator;
for(var ii=0;ii<result.length;ii++){
iterator = decodeURIComponent(result[ii]).split("=");
if(iterator[0]!="" && typeof iterator[0]!=null){
object[iterator[0]] = iterator[1];
}
}
return object;
};
})();
console.log(uri.serialize({"Content-type": "application/json", "Connection": "Keep-Alive"}));
console.log(uri.deserialize(uri.serialize({"Content-type": "application/json", "Connection": "Keep-Alive"})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment