json-stringify-util
v1.1.0
Published
A Node.js module designed to handle the serialization and deserialization of JavaScript objects, including functions.
Downloads
8
Maintainers
Keywords
Readme
JSON Stringify Utility
A Node.js module for serializing and deserializing JavaScript objects containing functions.
Installation
You can install this module via npm:
npm install --save json-stringify-util
Usage in CommonJS
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();
Usage in ES Modules
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();
API
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.