Package Exports
- json-refs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-refs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
json-refs
Various utilities for JSON References, and JSON Pointers since JSON References are part JSON Pointer.
Project Badges
Installation
json-refs is available for both Node.js and the browser. Installation instructions for each environment are below.
Browser
Installation for browser applications can be done via [Bower][bower] or by downloading a standalone binary.
Using Bower
bower install json-refs --save
Standalone Binaries
The standalone binaries come in two flavors:
- json-refs-standalone.js: 692kb, full source and source maps
- json-refs-standalone-min.js: 48kb, minified, compressed and no sourcemap
Node.js
Installation for Node.js applications can be done via [NPM][npm].
npm install json-refs --save
APIs
All examples below use a variable called jsonRefs
. Here is how to create it in Node.js:
var jsRefs = require('jsonRefs');
For the browser, JsonRefs
is exported.
findRefs (json)
Arguments
json {object}
- The JavaScript object to search for references
Response
An object
whose keys are JSON Pointers to where the JSON Reference's $ref
node is and the JSON Reference string
.
isJsonReference (obj)
Arguments
[obj] {*}
- The object to check
Response
true
if the argument is an object
and its $ref
property is a JSON Pointer and false
otherwise.
isRemotePointer (ptr)
Arguments
ptr {*}
- The JSON Pointer to check
Response
true
if the argument is an is a JSON Pointer to a remote document and false
otherwise.
pathFromPointer (ptr)
Arguments
ptr {string}
- A JSON Pointer string
Response
A string[]
of path segments for the JSON Pointer unless its a remote reference in which case ptr
is returned as-is.
Example
console.log(jsonRefs.pathFromPointer('#/owner/login')); // ['owner', 'login']
pathToPointer (path)
Arguments
path {string[]}
- An array of path segments.
Response
A string
representing a JSON Pointer.
Example
console.log(jsonRefs.pathToPointer(['owner', 'login'])); // #/owner/login
resolveRefs (json, done)
Arguments
json {object}
: The JavaScript object containing zero or more JSON Referencesdone {function}
: An error-first callback to be called with the fully-resolved object
Response
If there is an Error
, the callback is called with the Error
in the first argument and undefined
in the second
argument. If there is no Error
, the first argument is undefined
and the second argument is an object
whose value
is the fully resolved document.
Example
var json = {
name: 'json-refs',
owner: {
$ref: 'https://api.github.com/repos/whitlockjc/json-refs#/owner'
}
};
jsonRefs.resolveRefs(json, function (err, rJson) {
if (err) throw err;
console.log(JSON.stringify(rJson)); // {name: 'json-refs', owner: {/* GitHub Repository Owner Information */}}
});