Serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.
Notice: This module is fork from circular-json which is already deprecated, we don’t change anything of it but remove the deprecate message. Egg.js only use it to format and display application’s configurations.
NOTE: Platforms without native JSON (i.e. MSIE <= 8) requires json3.js or similar.
It is also a bad idea to CircularJSON.parse(JSON.stringify(object)) because of those manipulation used in CircularJSON.stringify() able to make parsing safe and secure.
As summary: CircularJSON.parse(CircularJSON.stringify(object)) is the way to go, same is for JSON.parse(JSON.stringify(object)).
API
It’s the same as native JSON, except the fourth parameter placeholder, which circular references to be replaced with "[Circular]" (i.e. for logging).
The module json-stringify-safe seems to be for console.log() but it’s completely pointless for JSON.parse(), being latter one unable to retrieve back the initial structure. Here an example:
// a logged object with circular references
{
"circularRef": "[Circular]",
"list": [
"[Circular]",
"[Circular]"
]
}
// what do we do with above output ?
Just type this in your node console: var o = {}; o.a = o; console.log(o);. The output will be { a: [Circular] } … good, but that ain’t really solving the problem.
However, if that’s all you need, the function used to create that kind of output is probably faster than CircularJSON and surely fits in less lines of code.
Why Not {{put random name}} Solution
So here the thing: circular references can be wrong but, if there is a need for them, any attempt to ignore them or remove them can be considered just a failure.
Not because the method is bad or it’s not working, simply because the circular info, the one we needed and used in the first place, is lost!
In this case, CircularJSON does even more than just solve circular and recursions: it maps all same objects so that less memory is used as well on deserialization as less bandwidth too!
It’s able to redefine those references back later on so the way we store is the way we retrieve and in a reasonably performant way, also trusting the snappy and native JSON methods to iterate.
CircularJSON
Serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.
Notice: This module is fork from circular-json which is already deprecated, we don’t change anything of it but remove the deprecate message. Egg.js only use it to format and display application’s configurations.
The future of this module is called flatted
Smaller, faster, and able to produce on average a reduced output too, flatted is the new, bloatless, ESM and CJS compatible, circular JSON parser.
It has now reached V1 and it implements the exact same JSON API.
Please note CircularJSON is in maintenance only and flatted is its successor.
A Working Solution To A Common Problem
A usage example:
A quick summary:
0.5, you can specify a JSON parser different from JSON itself.CircularJSON.parser = ABetterJSON;is all you need.~as a special prefix symbol to denote which parent the reference belongs to (i.e.~root~child1~child2)Node Installation & Usage
There are no dependencies.
Browser Installation & Usage
(generated via gitstrap)
NOTE: Platforms without native JSON (i.e. MSIE <= 8) requires
json3.jsor similar.It is also a bad idea to
CircularJSON.parse(JSON.stringify(object))because of those manipulation used inCircularJSON.stringify()able to make parsing safe and secure.As summary:
CircularJSON.parse(CircularJSON.stringify(object))is the way to go, same is forJSON.parse(JSON.stringify(object)).API
It’s the same as native JSON, except the fourth parameter
placeholder, which circular references to be replaced with"[Circular]"(i.e. for logging).Bear in mind
JSON.parse(CircularJSON.stringify(object))will work but not produce the expected output.Similar Libraries
Why Not the @izs One
The module json-stringify-safe seems to be for
console.log()but it’s completely pointless forJSON.parse(), being latter one unable to retrieve back the initial structure. Here an example:Just type this in your
nodeconsole:var o = {}; o.a = o; console.log(o);. The output will be{ a: [Circular] }… good, but that ain’t really solving the problem.However, if that’s all you need, the function used to create that kind of output is probably faster than
CircularJSONand surely fits in less lines of code.Why Not {{put random name}} Solution
So here the thing: circular references can be wrong but, if there is a need for them, any attempt to ignore them or remove them can be considered just a failure.
Not because the method is bad or it’s not working, simply because the circular info, the one we needed and used in the first place, is lost!
In this case,
CircularJSONdoes even more than just solve circular and recursions: it maps all same objects so that less memory is used as well on deserialization as less bandwidth too! It’s able to redefine those references back later on so the way we store is the way we retrieve and in a reasonably performant way, also trusting the snappy and nativeJSONmethods to iterate.