-
Notifications
You must be signed in to change notification settings - Fork 0
Storing Functions in the JSON files (Solved) JavaScript NodeJS
nexssp edited this page Jan 22, 2020
·
1 revision
I was trying to find solution to store also functions in Json.
const myObject = {
x: 1,
y: "Test",
fff: function(x) {
return x + 1000000;
}
};
let jsonString = JSON.stringify(myObject, (k, v) =>
typeof v === "function" ? "" + v : v
);
console.log(jsonString);
let parsedJson = eval(
JSON.parse(jsonString, function(k, v) {
if (typeof v === "string" && v.startsWith("function(") && v.endsWith("}")) {
return eval("(" + v + ")");
}
return v;
})
);
console.log(parsedJson.fff(1234));
based on: https://stackoverflow.com/questions/36517173/how-to-store-a-javascript-function-in-json
NodeJS/JavaScript implementation for Nexss Programmer