Name: circular-json
Owner: Postman
Description: JSON does not handle circular references. Now it does
Forked from: WebReflection/circular-json
Created: 2017-03-09 11:19:42.0
Updated: 2017-03-09 11:19:44.0
Pushed: 2016-08-11 08:40:19.0
Homepage: null
Size: 2047
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.
A usage example:
object = {};
ct.arr = [
ject, object
ct.arr.push(object.arr);
ct.obj = object;
serialized = CircularJSON.stringify(object);
{"arr":["~","~","~arr"],"obj":"~"}'
OTE: CircularJSON DOES NOT parse JS
t handles receiver and reviver callbacks
unserialized = CircularJSON.parse(serialized);
arr: [ [Circular], [Circular] ],
bj: [Circular] }
rialized.obj === unserialized;
rialized.arr[0] === unserialized;
rialized.arr.pop() === unserialized.arr;
A quick summary:
~
as a special prefix symbol to denote which parent the reference belongs to (i.e. ~root~child1~child2
)install --save circular-json
avascript
strict';
CircularJSON = require('circular-json')
obj = { foo: 'bar' }
str
self = obj;
= CircularJSON.stringify(obj);
There are no dependencies.
(generated via gitstrap)
ipt src="build/circular-json.js"></script>
avascript
strict';
CircularJSON = window.CircularJSON
obj = { foo: 'bar' }
str
self = obj;
= CircularJSON.stringify(obj);
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))
.
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.
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:
logged object with circular references
ircularRef": "[Circular]",
ist": [
"[Circular]",
"[Circular]"
hat 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.
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.