Created
December 14, 2012 00:35
-
-
Save javierhonduco/4281425 to your computer and use it in GitHub Desktop.
Url serialization helpers in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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