Created
May 30, 2013 21:22
-
-
Save brunobord/5681346 to your computer and use it in GitHub Desktop.
localStorage (de)serialization via loads and dumps prototype functions
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
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