forked from caldwell/renderjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle id/ref in json caldwell#35
- Loading branch information
Showing
2 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Usage | ||
// ----- | ||
// The module exports one entry point, the `resovlejson()` function. It takes in | ||
// the JSON that has id/ref that you want to render as a single argument and returns JSON | ||
// with whatever graph cycles that are in the document. Note that typically you can't | ||
// fully expand the json as its a graph. But when navigating via the + sign it is what | ||
// you would expect. | ||
// | ||
// There are two use cases, | ||
// the first is microsoft's encodes it's graphs with $id $ref, to avoid cyclic graphs | ||
// (or just to save space). This is resolved renderjson(resolvejson("$id","$idref", ... | ||
// | ||
// the second is for json schema's they encode their graphs with id $ref | ||
// These can be resolved by renderjson(resolvejson("id", "$ref", ... | ||
var resolvejson = (function () { | ||
var resolvejson = function resolvejson(idProp, refProp, json) { | ||
let byid = {}; //dictionary to keep the reference objects | ||
(function collectObjIds(o) { | ||
if (o.hasOwnProperty(idProp)) { | ||
let id = o[idProp]; | ||
byid[id] = o; | ||
} | ||
for (var i in o) { | ||
if (o[i] !== null && typeof (o[i]) == "object") | ||
collectObjIds(o[i]); | ||
} | ||
})(json) | ||
json = (function replaceRefs(obj) { | ||
if (typeof obj !== 'object' || !obj) // a primitive value | ||
return obj; | ||
if (obj.hasOwnProperty(refProp)) { | ||
let ref = obj[refProp]; | ||
//we will always have the ref stored in the map | ||
if (ref in byid) { | ||
return byid[ref]; | ||
} else { | ||
return obj | ||
} | ||
} | ||
if (Array.isArray(obj)) { | ||
obj = obj.map(replaceRefs); | ||
} else { | ||
for (let prop in obj) { | ||
obj[prop] = replaceRefs(obj[prop]); | ||
} | ||
} | ||
return obj; | ||
})(json); | ||
|
||
return json; | ||
} | ||
return resolvejson; | ||
})(); | ||
|
||
if (define) define({resolvejson: resolvejson}) | ||
else (module || {}).exports = (window || {}).resolvejson = resolvejson; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters