Skip to content

Instantly share code, notes, and snippets.

@brunobord
Created May 30, 2013 21:22
Show Gist options
  • Save brunobord/5681346 to your computer and use it in GitHub Desktop.
Save brunobord/5681346 to your computer and use it in GitHub Desktop.
localStorage (de)serialization via loads and dumps prototype functions
Object.getPrototypeOf(localStorage).dumps = function() {
return JSON.stringify(this);
};
Object.getPrototypeOf(localStorage).loads = function(str, clear) {
if (clear == true)
this.clear();
var doc = JSON.parse(str);
for (var i = 0; i < Object.keys(doc).length; i++) {
key = Object.keys(doc)[i];
this.setItem(key, doc[key]);
};
};
/* Usage:
* var str = localStorage.dumps()
* localStorage.loads(str);
* **WARNING** If you add the 'clear' argument, the whole localStorage keys will be flushed away
* localStorage.loads(str, true);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment