Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const docNQuads =
`_: "Manu Sporny" .
_: .
_: .`;
const context = {
name: "http://schema.org/name",
homepage: {"@id": "http://schema.org/url", "@type": "@id"},
image: {"@id": "http://schema.org/image", "@type": "@id"}
};
const baseUrl = 'http://schema.org';
const frame = doc;
const docRDF = jsonld.toRDF(doc);
let count = 0;
function log(doc: object|string) {
count++;
// Uncomment if testing with node.js
if (typeof doc === 'object') {
// console.log(count + ": " + JSON.stringify(doc) + "\n");
} else {
// console.log(doc);
}
}
/**
* compact() test
*/
jsonld.compact(doc, context, (err, compDoc) => {
return new Promise(function (resolve, reject) {
var struct = JSON.parse(text);
Jsonld.toRDF(struct, {format: "application/nquads", base: url}, function (lderr, nquads) {
if (lderr) {
reject("error parsing JSON-ld " + url + ": " + lderr);
} else {
meta.prefixes = {}; // @@ take from @context?
meta.base = url; // @@ take from @context.base? (or vocab?)
resolve(parseTurtle(nquads, mediaType, url, data, meta));
}
});
});
}
} else if (contentType === 'application/sparql-update') { // @@ we handle a subset
sparqlUpdateParser(str, kb, base)
executeCallback()
} else if (contentType === 'application/ld+json' ||
contentType === 'application/nquads' ||
contentType === 'application/n-quads') {
var n3Parser = N3.Parser()
var triples = []
if (contentType === 'application/ld+json') {
var jsonDocument
try {
jsonDocument = JSON.parse(str)
} catch (parseErr) {
return callback(parseErr, null)
}
jsonld.toRDF(jsonDocument,
{format: 'application/nquads', base},
nquadCallback)
} else {
nquadCallback(null, str)
}
} else {
throw new Error("Don't know how to parse " + contentType + ' yet')
}
} catch (e) {
executeErrorCallback(e)
}
function executeCallback () {
if (callback) {
callback(null, kb)
} else {
testExamples("JSON-LD examples", types, (objects, file, type) => {
objects.forEach( doc => {
doc['@context'] = context
cleanup(doc)
})
jsonld.toRDF(objects, {format: 'application/n-quads'}, (err, nquads) => {
ntfile = file.replace(/\.json/,'.nt')
nquads = nquads.split("\n").filter( l => l ).sort().join("\n")+"\n"
if (fs.existsSync(ntfile)) {
let expect = fs.readFileSync(ntfile).toString()
assert.equal(expect, nquads)
} else {
console.log(nquads)
}
})
})
});
jsonld.fromRDF(docRDF)
.then((res) => {
log(res);
});
jsonld.fromRDF(docRDF, {useRdfType: false})
.then((res) => {
log(res);
});
/**
* toRDF() test
*/
jsonld.toRDF(doc, (err, res) => {
log(res);
});
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, res) => {
log(res);
});
jsonld.toRDF(doc)
.then((res) => {
log(res);
});
jsonld.toRDF(doc, {produceGeneralizedRdf: false})
.then((res) => {
log(res);
});
jsonld.expand(obj, function(err, expanded) {
if (err) {
return callback && callback(err);
}
if (options.base) {
if (expanded['@context']) {
expanded['@context']['@base'] = options.base;
} else {
expanded['@context'] = { '@base' : options.base };
}
}
jsonld.toRDF(expanded, options, function(err, triples) {
if (err || triples.length === 0) {
return callback(err, null);
}
var stream = graphdb.putStream();
stream.on('error', callback);
stream.on('close', function() {
var blank_keys = Object.keys(blanks);
var clone_obj = Object.assign({}, obj)
var frame;
frame = (function framify(o) {
Object.keys(o).map(function(key) {
if (Array.isArray(o[key]) && key != "@type") {
function extractTriples(json, addTriple, done) {
jsonld.toRDF(json, function (error, triples) {
for (var graphName in triples) {
triples[graphName].forEach(function (triple) {
addTriple(triple.subject.value,
triple.predicate.value,
convertEntity(triple.object));
});
}
done(error);
});
}
function parse(rawDocument, contentType, convertCallback) {
var n3Parser = N3.Parser();
var n3Writer;
var triples = [];
var prefixes = {};
if (contentType === 'application/ld+json') {
var jsonDocument;
try {
jsonDocument = JSON.parse(rawDocument);
} catch (err) {
convertCallback(err, null);
}
jsonld.toRDF(jsonDocument, {
format: 'application/nquads'
}, nquadCallback);
} else if (contentType === 'application/nquads' ||
contentType === 'application/n-quads') {
nquadCallback(null, rawDocument);
} else {
convertCallback(new Error("Wrong content type"), null);
}
function nquadCallback(err, nquads) {
if (err) {
debug("Error parsing nquads: " + err);
convertCallback(err, null);
}
try {
n3Parser.parse(nquads, tripleCallback, prefixCallback);
parse (document, callback) {
jsonld.toRDF(JSON.parse(document), {base: this.documentIRI}, (error, quads) => {
for (var index in quads) {
var quad = quads[index];
callback(null, this.addEqualsMethodToQuad(quad));
if (index == quads.length-1 ) {
//This is the end
callback(null, null);
}
}
if (error) {
callback(error);
}
});
}
}