Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Last active May 2, 2017 22:26
Show Gist options
  • Save vyach-vasiliev/64ce527c48ed01d2928fa8f449c378bd to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/64ce527c48ed01d2928fa8f449c378bd to your computer and use it in GitHub Desktop.
localStorage or sessionStorage to json, json to localStorage or sessionStorage, localStorage2json, json2localStorage, sessionStorage2json, json2sessionStorage
function storage2json(storage){
/* storage2json, v1.5, Copytight 2017, MIT License, Vyach.Vasiliev
The MIT License
Copyright (c) 2017 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var res_obj = {},
re_pass = /([\{\}\[\]\"\']|^\d+$|^(true|false)$)/,
parse = function(value){
if(value.match(re_pass)){
return JSON.parse(value);
}else{
return JSON.parse('"'+ value +'"');
}
};
for (var i=0, len=storage.length; i<len; i++) {
res_obj[storage.key(i)] = parse(storage.getItem(storage.key(i)));
}
return JSON.stringify(res_obj)
}
function json2storage(json_data, storage){
/* json2storage, v1.5, Copytight 2017, MIT License, Vyach.Vasiliev
The MIT License
Copyright (c) 2017 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var res_obj = JSON.parse(json_data),
re_pass = /([\{\}\[\]\"\']|^\d+$|^(true|false)$)/,
parse = function(value){
if(value.match(re_pass)){
return JSON.parse(value);
}else{
return JSON.parse('"'+ value +'"');
}
};
for (var prop in res_obj) {
if (res_obj.hasOwnProperty(prop)) {
storage.setItem(prop, JSON.stringify(res_obj[prop]));
}
}
return true;
}
/* MINIFY */
function storage2json(e){/* Copyright (c) 2017 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com).Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ for(var r={},t=/([\{\}\[\]\"\']|^\d+$|^(true|false)$)/,n=function(e){return e.match(t)?JSON.parse(e):JSON.parse('"'+e+'"')},a=0,s=e.length;s>a;a++)r[e.key(a)]=n(e.getItem(e.key(a)));return JSON.stringify(r)}
function json2storage(r,n){/* Copyright (c) 2017 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com).Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */var t=JSON.parse(r);for(var e in t)t.hasOwnProperty(e)&&n.setItem(e,JSON.stringify(t[e]));return!0}
/* HOW USAGE */
var storage = window.localStorage; // localStorage or sessionStorage
var b = storage2json(storage);
json2storage(b, storage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment