delite-stringify
v0.0.4
Published
A lightweight library for in replace of stringify.
Maintainers
Readme
Delite Stringify
A lightweight and powerful JSON serializer/deserializer that safely handles special JavaScript values that JSON.stringify() normally breaks.
✨ Features
Supports:
- ✅ undefined
- ✅ NaN
- ✅ Infinity
- ✅ -Infinity
- ✅ -0
- ✅ BigInt
- ✅ Date
- ✅ Symbol
- ✅ RegExp
- ✅ Set
- ✅ Map
- ✅ Circular References
Zero dependencies. Works in Node.js and Browser.
📦 Installation
npm install delite-stringifyOr scoped:
npm install @ganesh123as/delite-stringify🤔 Why Not JSON.stringify?
Normal JSON has limitations:
JSON.stringify({
undef: undefined,
nan: NaN,
infinity: Infinity,
big: 10n,
set: new Set([1, 2])
});Problems:
- ❌ undefined is removed
- ❌ NaN becomes null
- ❌ Infinity becomes null
- ❌ BigInt throws error
- ❌ Set becomes {}
- ❌ Circular references crash
👉 Delite Stringify solves all of this.
Basic Usage
import { stringify, parse } from "delite-stringify";
const data = {
name: "Ganesh",
big: 12345678901234567890n,
date: new Date(),
set: new Set([1, 2, 3]),
map: new Map([["a", 1]]),
undef: undefined,
nan: NaN
};
const str = stringify(data);
const restored = parse<typeof data>(str);
console.log(restored);Everything is restored correctly 🎉
🔄 Circular Reference Example
const obj: any = { name: "test" };
obj.self = obj;
const str = stringify(obj);
const restored = parse(str);
console.log(restored.self === restored); // trueNo crash. Fully restored.
📚 API
stringify(value)
Works like JSON.stringify() but supports advanced types.
const str = stringify(value);parse(string)
Works like JSON.parse() but restores special types.
const data = parse<MyType>(stringValue);serialize(value)
Returns structured data:
{
json: any,
meta?: {
values: Record<string, any>,
v: 1
}
}Used internally by stringify().
deserialize(data)
Restores original value from serialized data.
Used internally by parse().
🔥 Supported Types
| Type | Supported | |-----------------|-----------| | undefined | ✅ | | NaN | ✅ | | Infinity | ✅ | | -Infinity | ✅ | | -0 | ✅ | | BigInt | ✅ | | Date | ✅ | | Symbol | ✅ | | RegExp | ✅ | | Set | ✅ | | Map | ✅ | | Circular Ref | ✅ |
⚡ How It Works
- Recursively walks through your object.
- Converts special values into safe JSON format.
- Stores type information in metadata.
- Restores everything back during parsing.
📄 License
MIT
👨💻 Author
Ganesh Bhatt
If you found this useful, ⭐ star the repository!
