Saving a Javascript object to a file
file, javascript, object, save
Solution
Despite all the answers to the contrary, this is indeed possible. However it is limited by browser support. You can use the new FileSystem APIs in the latest versions of Chrome, etc:
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.PERSISTENT, 1024, function(fs) {
fs.root.getFile('mystorage.txt', {create: true, exclusive: true}, function(file) {
file.createWriter(function(writer) {
var blob = new Blob(["putdatahere"], {type: 'text/plain'});
writer.write(blob);
});
});
}, function() {
console.log("Could not access file system");
});
Since you only want the files for your own uses, this sandboxed approach will work. There are a few more hoops you need to jump through (requesting a quota, creating a `Blob` object) but those are covered by the linked article and will all depend on your requirements.
Problem
I need to save a Javascript object to a file, so it can be easily reloaded into a variable, modified and eventually re-saved. Is this feasible in Javascript and if so what's the best way to approach it. For example if I have: ``` o = {"test": 2}; ``` How can I save it to a file, load, modify and save it? Exactly like `pickle` in Python. Thanks.