explaingit

douglascrockford/json-js

8,731JavaScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

Two small JavaScript files by the creator of JSON: a now-obsolete browser polyfill and cycle.js, which lets you serialize and restore objects that contain circular references without crashing.

Mindmap

mindmap
  root((json-js))
    Files
      json2.js historical polyfill
      cycle.js circular refs
    What cycle.js does
      Encode circular links
      Replace with path strings
      Decode back to objects
    Use Cases
      Serialize object graphs
      Parent-child trees
      Doubly-linked lists
    Context
      Created by JSON inventor
      No dependencies
    Audience
      JavaScript developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Handle JSON serialization of objects that contain circular references using cycle.js without getting a TypeError.

USE CASE 2

Restore circular-reference structures from JSON by decoding the path strings back into live object links.

USE CASE 3

Safely serialize complex data structures like doubly-linked lists or trees with parent pointers to JSON.

USE CASE 4

Study how the JSON standard was originally polyfilled for browsers before native support was added in 2009.

Tech stack

JavaScript

Getting it running

Difficulty · easy Time to first run · 5min
No license information is mentioned in the explanation.

In plain English

This repository is maintained by Douglas Crockford, the person who created the JSON data format. JSON, which stands for JavaScript Object Notation, is the most common way to exchange data between web applications and servers today. It was originally invented as a subset of JavaScript, and this repository contains some of Crockford's original JavaScript implementations related to JSON. The repository includes two files. The first, json2.js, is a historical polyfill: a piece of code that adds JSON support to very old browsers that did not have it built in. Modern browsers have native JSON support built into the language itself, added as a standard feature in 2009. The README states plainly that json2.js does nothing on current browsers and that the only reason to use it would be to support Internet Explorer 8, which it then says no one should ever have to do again. This file is essentially a historical artifact. The second file, cycle.js, is more practically useful in certain situations. It contains two functions for handling circular references in JSON. A circular reference is when an object points back to itself somewhere in its structure, like a tree node that has a reference to its own parent. Standard JSON cannot represent circular references and will throw an error when you try to convert such a structure. cycle.js adds the ability to encode those structures by replacing the circular links with path strings, and to decode them back when reading the data. This is a small, focused utility repository with no build process and no dependencies. Its significance comes mainly from its author and its role in the early history of JSON as a standard.

Copy-paste prompts

Prompt 1
My JavaScript object has circular references and JSON.stringify throws an error. Show me how to use douglascrockford/json-js cycle.js to serialize and deserialize it safely.
Prompt 2
Explain how cycle.js from douglascrockford/json-js encodes circular references as path strings. Walk me through a small example with a parent-child tree node.
Prompt 3
I have a linked list where each node has a next and prev pointer. Write code using cycle.js to serialize this list to JSON and reconstruct it.
Prompt 4
When should I use cycle.js versus other circular-reference JSON libraries like flatted? What are the trade-offs?
Open on GitHub → Explain another repo

← douglascrockford on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.