Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
json-stringify-safe
Advanced tools
The json-stringify-safe package is a JSON serialization library that can stringify objects with circular references without throwing an error. It is particularly useful when dealing with complex objects where standard JSON.stringify() would fail due to circularity.
Stringify with circular references
This feature allows you to serialize objects that contain circular references by replacing the circular reference with a placeholder string that indicates the path of the circularity.
{"str": "Circular reference example", "obj": {"a": "b", "c": {"d": "[Circular ~.obj]"}}}
Custom replacer function
json-stringify-safe allows you to specify a custom replacer function to selectively serialize object properties, similar to the second argument of JSON.stringify().
{"str": "Custom replacer example", "obj": {"a": "b", "c": "[Filtered]"}}
Custom indentation
The package also supports custom indentation for the output string, allowing for more readable serialized JSON if needed.
{
"str": "Indented output example",
"obj": {
"a": "b",
"c": "d"
}
}
Flatted is a package that uses a similar approach to json-stringify-safe, but instead of replacing circular references with a placeholder, it flattens the structure into an array. This can be useful for certain serialization and deserialization needs.
json-cycle is another package that deals with circular references by encoding them using the Douglas Crockford's cycle.js algorithm. It can stringify and then restore objects with circular references to their original state.
safe-stable-stringify is a package that ensures deterministic serialization of objects, including those with circular references. It is similar to json-stringify-safe but also guarantees the order of keys, which can be important for hashing and caching.
Like JSON.stringify, but doesn't throw on circular references.
Takes the same arguments as JSON.stringify
.
var stringify = require('json-stringify-safe');
var circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [ circularObj, circularObj ];
console.log(stringify(circularObj, null, 2));
Output:
{
"circularRef": "[Circular]",
"list": [
"[Circular]",
"[Circular]"
]
}
stringify(obj, serializer, indent, decycler)
The first three arguments are the same as to JSON.stringify. The last is an argument that's only used when the object has been seen already.
The default decycler
function returns the string '[Circular]'
.
If, for example, you pass in function(k,v){}
(return nothing) then it
will prune cycles. If you pass in function(k,v){ return {foo: 'bar'}}
,
then cyclical objects will always be represented as {"foo":"bar"}
in
the result.
stringify.getSerialize(serializer, decycler)
Returns a serializer that can be used elsewhere. This is the actual function that's passed to JSON.stringify.
Note that the function returned from getSerialize
is stateful for now, so
do not use it more than once.
5.0.1 (May 19, 2015)
replacer
function in the proper context (thisArg
).cycleReplacer
function in the proper context (thisArg
).FAQs
Like JSON.stringify, but doesn't blow up on circular refs.
The npm package json-stringify-safe receives a total of 21,658,367 weekly downloads. As such, json-stringify-safe popularity was classified as popular.
We found that json-stringify-safe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.