Encoding and decoding of circular data structures for converting to and from JSON.
npm install @lnu/json-js-cyclecycle.cs file from Douglas Crockford's JSON-js repository bash
npm install @lnu/json-js-cycle
`
Usage
`javascript
import '@lnu/json-js-cycle'
`
$3
Make a deep copy of an object or array, assuring that there is at most one instance of each object or array in the resulting structure. The duplicate references (which might be forming cycles) are replaced with an object. The property names are sorted.
#### Options
includeNonEnumerableProperties - If set to true, non-enumerable properties will be included in the result. By default, the includeNonEnumerableProperties attribute is set to false.
replacer - A function that transforms values. The function is called for each item and should return the value to be used instead of the original value. If the original value is to be used, the function should return the value it received. By default, the replacer attribute is not set.
#### Examples
The duplicate references (which might be forming cycles) are replaced with an object of the form {"$ref": PATH}, where the PATH is a JSONPath string that identifies the first occurrence.
`javascript
// The example below is taken from the original cycle.js file.
const a = []
a[0] = a
console.log(JSON.stringify(JSON.decycle(a)))
// output: [{"$ref":"$"}]
`
By default, the decycle function excludes non-enumerable properties. However, if you set the includeNonEnumerableProperties option to true, it will include these properties in the result.
`javascript
const error = new Error('Something went really wrong.')
error.status = 500
console.log(JSON.stringify(
JSON.decycle(error))
)
// output: {"status":500}
console.log(JSON.stringify(
JSON.decycle(error, { includeNonEnumerableProperties: true }))
)
// output: {"message":"Something went really wrong.","stack":"Error: Something went really wrong.\n at file:[Omitted for clarity]","status":500}
``