A Node.js module for serializing and deserializing JavaScript objects containing functions.
You can install this module via npm:
npm install --save json-stringify-util
Requiring the Module
const { JSONUtil } = require("json-stringify-util");
Example Usage
Stringify an Object with Functions
const obj1 = {
prop1: "value",
prop2: () => {
console.log("This is a function");
},
};
// Stringifies the object
const jsonString = JSONUtil.stringify(obj1);
// Outputs: {"prop1":"value","prop2":"() => {\n console.log(\"This is a function\");\n }"}
console.log(jsonString);
Parse a JSON String with Functions
// Parses the JSON string
const obj2 = JSONUtil.parse(jsonString);
// Invokes the function
// Outputs: "This is a function"
obj2.prop2();
Importing the Module
import { JSONUtil } from "json-stringify-util";
Example Usage
Stringify an Object with Functions
const obj1 = {
prop1: "value",
prop2: () => {
console.log("This is a function");
},
};
// Stringifies the object
const jsonString = JSONUtil.stringify(obj1);
// Outputs: {"prop1":"value","prop2":"() => {\n console.log(\"This is a function\");\n }"}
console.log(jsonString);
Parse a JSON String with Functions
// Parses the JSON string
const obj2 = JSONUtil.parse(jsonString);
// Invokes the function
// Outputs: "This is a function"
obj2.prop2();
JSONUtil.stringify(obj)
- Serializes an object with functions to JSON format.
- Returns a JSON string representing the object.
JSONUtil.parse(jsonString)
- Parses a JSON string containing functions back into an object.
- Returns the parsed object with functions intact.