0

I have a neural network library that I'm creating that contains nested objects stored in arrays. I need to be able to save the states of these objects to local storage. I've attempted to use JSON.stringify to convert the highest level network object into something I can save, but when I JSON.parse() it back, it doesn't contain the methods.

The code can be found here.

Thanks in advance for your help.

Shadow
  • 8,749
  • 4
  • 47
  • 57
  • 4
    You cannot store functions in local storage. You will want to [revive your objects](https://stackoverflow.com/q/11810028/1048572). – Bergi Dec 12 '17 at 03:03
  • To add to @Bergi's answer, you shouldn't need to store functions in localStorage either. If your functions are written in such a way that they have data baked in, it should be possible to rewrite whatever generates these functions in such a way that the data is what is stored, and the function is independent. – Jake Dec 12 '17 at 03:04
  • Please don't add a link to your GitHub project, instead add the relevant code here in an [mcve]. It makes it harder to see what code you're using and the GitHub repository will change in the future. Future readers will want to see the code that caused the problem now. – Michael M. Jul 02 '23 at 11:40

2 Answers2

0

Perhaps something like this would do the trick:

const rawData = JSON.parse(getFromLocalStorageMethod());
const data = new SomeType();
for (const key of rawData) {
    data[key] = rawData[key];
}

Or, if you don't know the type when restoring, you could probably store it too, just like this:

// in global for these two methods

const types = {
    1: SomeType
};

// in the save method

const data = new SomeType(...);
data.__type = 1;
saveToStorage(data);

// in the retrieve method

const json = JSON.parse(getFromLocalStorageMethod()); // you will have a json string anyway
const obj = new types[json.__type]();

if (json.__type === 1) {
    // do something with data depending on your type;
    // i think you get the idea at this point
}
blek__
  • 98
  • 1
  • 7
-1

Most of them won't recommend persisting function(behavior) inside JSON object, that is intended to carry only data. In case if you want to serialize the object with function, you need to override them, the below code would help you that.

This will help you to serialize function.

var json = function(obj){ return JSON.stringify(obj, function(key, value) {
  if (typeof value === 'function') {
    return value.toString();
  } else {
    return value;
  }
})};

I've created an object rama for testing, the output of json(rama) would be

 "{\"myName\":\"Ramasamy Kasiviswanathan\",\"myfunction\":\"function(input){\\nconsole.log('input', input);\\n}\"}"

Storing in localStorage,

localStorage.setItem('ramaLocal',json(rama));

Retrieving value from LocalStorage,

 ramadeserialize = eval("JSON.parse(localStorage.getItem('ramaLocal'))");

O/P would be:

Object { myName: "Ramasamy Kasiviswanathan", myfunction: "function(input){\nconsole.log('input', input);\n}" }

Reference: json.stringify does not process object methods