JSPM

json-decycle

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1316
  • Score
    100M100P100Q109631F
  • License GPL-2.0

(es6) stringify and parse cycled reference json

Package Exports

  • json-decycle
  • json-decycle/index
  • json-decycle/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-decycle) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

json-decycle (es6)

Stringify and parse cycled reference json by replacing cycled references to JSON-reference

Usage

var decycle = require('json-decycle').decycle
var retrocycle = require('json-decycle').retrocycle

var cycled = {
  foo: {},
  bar: {}
}

cycled.foo.bar = cycled.foo
cycled.bar.foo = cycled.bar

var result = JSON.stringify(cycled, decycle())
// result === '{"foo":{"bar":{"foo":{"$ref":"#/foo"}}},"bar":{"$ref":"#/foo/bar"}}'

JSON.parse(result, retrocycle())
// => {foo: {{foo: [cyclic reference], bar: [cyclic reference]}}, bar: {{foo: [cyclic reference], bar: [cyclic reference]}}}

Extend JSON global object

JSON.parse and JSON.stringify is not modified

require('json-decycle').extend(JSON)

JSON.decycle({}) === '{}'
JSON.retrocycle('{}') === {}

ES6 features

Library depends on es6 features Map, WeakMap, WeakSet and Set so if you environment does not support this features, you can override it:

const jsonDecycle = require('json-decycle')

jsonDecycle.Map = Map
jsonDecycle.WeakMap = WeakMap
jsonDecycle.WeakSet = WeakSet
jsonDecycle.Set = Set