# circular-ref-fix

> Adds $id fields to objects and replaces circular dependencies to recurring objects with $ref pointers, like Json.NET does.

Latest version **1.0.2** (published 2020-08-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install circular-ref-fix
pnpm add circular-ref-fix
yarn add circular-ref-fix
bun add circular-ref-fix
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-08-06 |
| First published | 2018-02-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Umut Özel |
| Maintainers | umutozel |
| Keywords | javascript, circular, json |

## Links

- npm: https://www.npmjs.com/package/circular-ref-fix
- Repository: https://github.com/DogusTeknoloji/circular-ref-fix
- Homepage: https://github.com/DogusTeknoloji/circular-ref-fix#readme
- Issues: https://github.com/DogusTeknoloji/circular-ref-fix/issues
- npm.io page: https://npm.io/package/circular-ref-fix

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-08-06
- 1.0.1 — 2018-02-23
- 1.0.0 — 2018-02-23

## README

# circular-ref-fix

[![Build Status](https://travis-ci.org/umutozel/circular-ref-fix.svg?branch=master)](https://travis-ci.org/umutozel/circular-ref-fix)
[![Coverage Status](https://coveralls.io/repos/github/umutozel/circular-ref-fix/badge.svg?branch=master)](https://coveralls.io/github/umutozel/circular-ref-fix?branch=master)
[![npm version](https://badge.fury.io/js/circular-ref-fix.svg)](https://badge.fury.io/js/circular-ref-fix)

:ferris_wheel: Fixes circular dependencies using $id identifiers and $ref pointers. Produces Json.NET friendly result.

---
## Usage

Let's model Simpsons family (Maggie hasn't born yet),

```JavaScript
    var child1 = {
        name: 'Bart'
    };
    var child2 = {
        name: 'Lisa'
    };
    var mother = {
        name: 'Marge'
    };
    var father = {
        name: 'Homer'
    };

    child1.siblings = [child2];
    child1.mother = mother;
    child1.father = father;

    child2.siblings = [child1];
    child2.mother = mother;
    child2.father = father;

    mother.spouse = father;
    mother.children = [child1, child2];

    father.spouse = mother;
    father.children = [child1, child2];
    
    JSON.stringify([mother, father]); // error! could not serialize
```
We create referential links between objects;

```JavaScript
var circularFix = require('circular-ref-fix');
var createRefs = circularFix.createRefs;

var fixed = createRefs(couple);
```
Produces below object;
```JavaScript
[  
   {  
      "$id":"1",
      "name":"Marge",
      "spouse":{  
         "$id":"2",
         "name":"Homer",
         "spouse":{  
            "$ref":"1"
         },
         "children":[  
            {  
               "$id":"3",
               "name":"Bart",
               "siblings":[  
                  {  
                     "$id":"4",
                     "name":"Lisa",
                     "siblings":[  
                        {  
                           "$ref":"3"
                        }
                     ],
                     "mother":{  
                        "$ref":"1"
                     },
                     "father":{  
                        "$ref":"2"
                     }
                  }
               ],
               "mother":{  
                  "$ref":"1"
               },
               "father":{  
                  "$ref":"2"
               }
            },
            {  
               "$ref":"4"
            }
         ]
      },
      "children":[  
         {  
            "$ref":"3"
         },
         {  
            "$ref":"4"
         }
      ]
   },
   {  
      "$ref":"2"
   }
]
```
Also, we can restore circular structure back;

```JavaScript
var circularFix = require('circular-ref-fix');
var restored = restoreRefs(fixed, true); // with true option, we tell restoreRefs to delete $id fields
```

Json.NET can handle this structure too;

```CSharp
var settings = new JsonSerializerSettings {
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

JsonConvert.DeserializeObject<List<Parent>>(fixedStr, settings);
```
---
### Node installation and usage

```JavaScript
npm i circular-ref-fix

var circularFix = require('circular-ref-fix');
```

---
Because we are using UMD pattern, you can use the *circular-fix.js* file on browser with script tag or Require.JS.

---
_Source: https://npm.io/package/circular-ref-fix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
