Skip to content

Bug/support nquads #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ jsonld.fromRDF(nquads, {format: 'application/nquads'}, function(err, doc) {
// doc is JSON-LD
});

// serialize a document to N-Quads (RDF)
jsonld.toRDF(doc, {format: 'application/n-quads'}, function(err, nquads) {
// nquads is a string of nquads
});

// deserialize N-Quads (RDF) to JSON-LD
jsonld.fromRDF(nquads, {format: 'application/n-quads', useStandardNQuadsType: true}, function(err, doc) {
// doc is JSON-LD
});

// register a custom async-callback-based RDF parser
jsonld.registerRDFParser = function(contentType, function(input, callback) {
// parse input to a jsonld.js RDF dataset object...
Expand Down
23 changes: 17 additions & 6 deletions js/jsonld.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,9 @@ jsonld.objectify = function(input, ctx, options, callback) {
* [base] the base IRI to use.
* [expandContext] a context to expand with.
* [inputFormat] the format if input is not JSON-LD:
* 'application/nquads' for N-Quads.
* 'application/nquads' or 'application/n-quads' for N-Quads.
* [format] the format if output is a string:
* 'application/nquads' for N-Quads.
* 'application/nquads' or 'application/n-quads' for N-Quads.
* [documentLoader(url, callback(err, remoteDoc))] the document loader.
* @param callback(err, normalized) called once the operation completes.
*/
Expand Down Expand Up @@ -788,7 +788,7 @@ jsonld.normalize = function(input, options, callback) {
}

if('inputFormat' in options) {
if(options.inputFormat !== 'application/nquads') {
if((options.inputFormat !== 'application/nquads') || (options.inputFormat !== 'application/n-quads')) {
Copy link
Member

@dlongley dlongley Aug 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be an &&? Otherwise it would seem that this compound conditional would always be true.

That being said, it would probably be cleaner to make a shallow copy of the options and if the inputFormat is application/nquads it should just be changed early to the correct application/n-quads. Then we only need to check for that in the code later. So let's normalize to application/n-quads.

return callback(new JsonLdError(
'Unknown normalization input format.',
'jsonld.NormalizeError'));
Expand Down Expand Up @@ -821,6 +821,8 @@ jsonld.normalize = function(input, options, callback) {
* @param [options] the options to use:
* [format] the format if dataset param must first be parsed:
* 'application/nquads' for N-Quads (default).
* [useStandardNQuadsType] the format of N-Quads
* (default: false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this option. All we're really doing is correcting for what could now be seen as a typo ... so if someone uses application/nquads for the inputFormat, let's just correct it for them and then proceed as if the proper format was given.

* [rdfParser] a custom RDF-parser to use to parse the dataset.
* [useRdfType] true to use rdf:type, false to use @type
* (default: false).
Expand Down Expand Up @@ -850,10 +852,18 @@ jsonld.fromRDF = function(dataset, options, callback) {
options.useNativeTypes = false;
}

if (!('useStandardNQuadsType' in options)){
options.useStandardNQuadsType = false;
}

if(!('format' in options) && _isString(dataset)) {
// set default format to nquads
if(!('format' in options)) {
options.format = 'application/nquads';
if (!options.useStandardNQuadsType){
options.format = 'application/nquads';
} else {
options.format = 'application/n-quads';
}
}
}

Expand Down Expand Up @@ -959,7 +969,7 @@ jsonld.toRDF = function(input, options, callback) {
// output RDF dataset
dataset = Processor.prototype.toRDF(expanded, options);
if(options.format) {
if(options.format === 'application/nquads') {
if((options.format === 'application/nquads') || (options.format === 'application/n-quads')) {
return callback(null, _toNQuads(dataset));
}
throw new JsonLdError(
Expand Down Expand Up @@ -4217,7 +4227,7 @@ Normalize.prototype.main = function(dataset, callback) {
normalized.sort();

// 8) Return the normalized dataset.
if(self.options.format === 'application/nquads') {
if((self.options.format === 'application/nquads') || (self.options.format === 'application/n-quads')) {
result = normalized.join('');
return callback();
}
Expand Down Expand Up @@ -7023,6 +7033,7 @@ function _parseNQuads(input) {

// register the N-Quads RDF parser
jsonld.registerRDFParser('application/nquads', _parseNQuads);
jsonld.registerRDFParser('application/n-quads', _parseNQuads);

/**
* Converts an RDF dataset to N-Quads.
Expand Down