util.obj-cycle
v0.0.9
Published
Monkey patched additional JSON functionality to handle cyclical objects
Downloads
195
Readme
util.obj-cycle
Monkey patched additional JSON functionality for cyclical objects
This module is a wrapper for cyclical object resolution using Douglas Crockford's JSON-js cycle code. It monkey patches two functions:
- JSON.decycle
- JSON.retrocycle
Installation
This module uses yarn to manage dependencies and run scripts for development.
To install as an application dependency with cli:
$ yarn add --dev util.obj-cycle
To build the app and run all tests:
$ yarn run all
Usage
To use this module, just use require to monkey patch it into the environment:
require('util.obj-cycle');
To remove the cycles from an object use JSON.decycle()
:
const i = {a: "x", ref: null};
const j = {b: "y", ref: null};
const k = {c: "z", ref: null};
i.ref = j;
j.ref = k;
k.ref = i;
let s: string = JSON.stringify(JSON.decycle(i));
/// s -> {"a":"x","ref":{"b":"y","ref":{"c":"z","ref":{"$ref":"$"}}}}
The process can be reversed on the object with JSON.retrocycle
:
let obj = JSON.retrocycle(JSON.parse(s));
The functions can also be included directly without the monkey patching by using import:
import {decycle, retrocycle} from "util.obj-cycle";
This is the same underlying code. Just a differnt way to do the same thing.