Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-cyclic

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-cyclic

Small utility library to replace circular references in JavaScript with JSONPaths

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

JSON Cyclic

CircleCI

A small JavaScript library to replace circular references in object literals with JSONPath references, so that the data can be stringified as JSON.

It also supports re-inserting the circular data after the JSON has been parsed.

This will fix the below TypeErrors:

Chrome: "TypeError: Converting circular structure to JSON"
Firefox: "TypeError: cyclic object value"
Edge: "TypeError: Circular reference in value argument not supported"
Safari: "TypeError: JSON.stringify cannot serialize cyclic structures."

Features

  • Accepts Arrays, Objects, or both in combination
  • Works with >IE8
  • Tiny (~1kB minified)
  • No dependencies

Installation

npm install json-cyclic

Usage

// ES2015
import { encycle, decycle } from "json-cyclic"

// CommonJS
const JSONCyclic = require("json-cyclic") 

API

There are only two methods exposed, and neither require any configuration:

decycle

Removes any circular data structures.

See the below usage examples:

// Arrays
const arr= [1, "a"]

arr[2] = arr;

JSON.stringify(decycle(arr)); // "{"foo":{"bar":{"$ref":"$.foo"}}}"

// Objects
const obj = { foo: { bar: null } };

obj.foo.bar = obj.foo;

JSON.stringify(decycle(obj)); // "{"foo":{"bar":{"$ref":"$.foo"}}}"

encycle

Re-inserts any circular data.

See the below:

const arr = [1, "a", { $ref: "$" }]

encycle(arr)

console.log(arr[2] === arr) // true

JSON.stringify(encycle(arr)); // TypeError: Converting circular structure to JSON

Tests

npm test

Keywords

FAQs

Package last updated on 26 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc