json-stringify-util

1.1.0 • Public • Published

JSON Stringify Utility

A Node.js module for serializing and deserializing JavaScript objects containing functions.

npm Downloads License

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.

Package Sidebar

Install

npm i json-stringify-util

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

9.98 kB

Total Files

11

Last publish

Collaborators

  • dasari.prakash