https://github.com/dogusteknoloji/circular-ref-fix
:ferris_wheel: Fixes circular dependencies using $id identifiers and $ref pointers. Produces Json.NET friendly result.
https://github.com/dogusteknoloji/circular-ref-fix
circular javascript json
Last synced: about 1 year ago
JSON representation
:ferris_wheel: Fixes circular dependencies using $id identifiers and $ref pointers. Produces Json.NET friendly result.
- Host: GitHub
- URL: https://github.com/dogusteknoloji/circular-ref-fix
- Owner: DogusTeknoloji
- License: mit
- Created: 2018-02-23T08:48:48.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-04T12:12:48.000Z (about 8 years ago)
- Last Synced: 2025-03-22T08:17:33.945Z (over 1 year ago)
- Topics: circular, javascript, json
- Language: JavaScript
- Homepage:
- Size: 67.4 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# circular-ref-fix
[](https://travis-ci.org/DogusTeknoloji/circular-ref-fix)
[](https://coveralls.io/github/DogusTeknoloji/circular-ref-fix?branch=master)
[](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>(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.