-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#349 Use jsonld package without n3 package
- Loading branch information
Fletcher91
committed
Oct 2, 2019
1 parent
10bc304
commit 069bae3
Showing
5 changed files
with
204 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,97 @@ | ||
import jsonld from 'jsonld' | ||
|
||
const rdf = { | ||
first: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', | ||
rest: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', | ||
nil: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil' | ||
} | ||
|
||
/** | ||
* Parses json-ld formatted JS objects to a rdf Term. | ||
* @param kb - The DataFactory to use. | ||
* @param obj - The json-ld object to process. | ||
* @return {Literal|Literal|Literal|NamedNode|NamedNode|NamedNode} | ||
*/ | ||
export function jsonldObjectToTerm (kb, obj) { | ||
if (typeof obj === 'string') { | ||
return kb.literal(obj) | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(obj, '@list')) { | ||
return listToStatements(kb, obj) | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(obj, '@id')) { | ||
return kb.namedNode(obj['@id']) | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(obj, '@language')) { | ||
return kb.literal(obj['@value'], obj['@language']) | ||
} | ||
|
||
if (Object.prototype.hasOwnProperty.call(obj, '@type')) { | ||
return kb.literal(obj['@value'], kb.namedNode(obj['@type'])) | ||
} | ||
|
||
return kb.literal(obj['@value']) | ||
} | ||
|
||
/** | ||
* Adds the statements in a json-ld list object to {kb}. | ||
*/ | ||
function listToStatements (kb, obj) { | ||
const listId = obj['@id'] ? kb.namedNode(obj['@id']) : kb.blankNode() | ||
|
||
obj['@list'].reduce((id, listObj, i, listArr) => { | ||
kb.addStatement(kb.quad(id, kb.namedNode(rdf.first), jsonldObjectToTerm(kb, listArr[i]))) | ||
|
||
let nextNode | ||
if (i < listArr.length - 1) { | ||
nextNode = kb.blankNode() | ||
kb.addStatement(kb.quad(id, kb.namedNode(rdf.rest), nextNode)) | ||
} else { | ||
kb.addStatement(kb.quad(id, kb.namedNode(rdf.rest), kb.namedNode(rdf.nil))) | ||
} | ||
|
||
return nextNode | ||
}, listId) | ||
|
||
return listId | ||
} | ||
|
||
/** | ||
* Takes a json-ld formatted string {str} and adds its statements to {kb}. | ||
* | ||
* Make sure that kb implements the DataFactory interface. | ||
*/ | ||
export default function jsonldParser (str, kb, base, callback) { | ||
const baseString = Object.prototype.hasOwnProperty.call(base, 'termType') | ||
? base.value | ||
: base | ||
|
||
return jsonld | ||
.flatten(JSON.parse(str), null, { base: baseString }) | ||
.then((flattened) => flattened.reduce((store, flatResource) => { | ||
const id = flatResource['@id'] | ||
? kb.namedNode(flatResource['@id']) | ||
: kb.blankNode() | ||
|
||
for (const property of Object.keys(flatResource)) { | ||
if (property === '@id') { | ||
continue | ||
} | ||
const value = flatResource[property] | ||
if (Array.isArray(value)) { | ||
for (let i = 0; i < value.length; i++) { | ||
kb.addStatement(kb.quad(id, kb.namedNode(property), jsonldObjectToTerm(kb, value[i]))) | ||
} | ||
} else { | ||
kb.addStatement(kb.quad(id, kb.namedNode(property), jsonldObjectToTerm(kb, value))) | ||
} | ||
} | ||
|
||
return kb | ||
}, kb)) | ||
.then(callback) | ||
.catch(callback) | ||
} |
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
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