Skip to content

Commit eef1932

Browse files
committed
Use application/n-quads format.
- Use the W3C standard MIME type for N-Quads: "application/n-quads". - See https://www.w3.org/TR/n-quads/#h2_sec-mediaReg - Also accept "application/nquads" for compatibility.
1 parent fd62168 commit eef1932

File tree

5 files changed

+156
-22
lines changed

5 files changed

+156
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# jsonld ChangeLog
22

3+
### Changed
4+
- Use the W3C standard MIME type for N-Quads of "application/n-quads". Accept
5+
"application/nquads" for compatibility.
6+
37
## 0.5.17 - 2018-01-25
48

59
### Changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,19 @@ jsonld.frame(doc, frame, (err, framed) => {
166166
// (URDNA2015), see: http://json-ld.github.io/normalization/spec/
167167
jsonld.canonize(doc, {
168168
algorithm: 'URDNA2015',
169-
format: 'application/nquads'
169+
format: 'application/n-quads'
170170
}, (err, canonized) => {
171171
// canonized is a string that is a canonical representation of the document
172172
// that can be used for hashing, comparison, etc.
173173
});
174174

175175
// serialize a document to N-Quads (RDF)
176-
jsonld.toRDF(doc, {format: 'application/nquads'}, (err, nquads) => {
177-
// nquads is a string of nquads
176+
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, nquads) => {
177+
// nquads is a string of N-Quads
178178
});
179179

180180
// deserialize N-Quads (RDF) to JSON-LD
181-
jsonld.fromRDF(nquads, {format: 'application/nquads'}, (err, doc) => {
181+
jsonld.fromRDF(nquads, {format: 'application/n-quads'}, (err, doc) => {
182182
// doc is JSON-LD
183183
});
184184

@@ -209,13 +209,13 @@ const flattened = await jsonld.flatten(doc);
209209
const framed = await jsonld.frame(doc, frame);
210210

211211
// canonicalization (normalization)
212-
const canonized = await jsonld.canonize(doc, {format: 'application/nquads'});
212+
const canonized = await jsonld.canonize(doc, {format: 'application/n-quads'});
213213

214214
// serialize to RDF
215-
const rdf = await jsonld.toRDF(doc, {format: 'application/nquads'});
215+
const rdf = await jsonld.toRDF(doc, {format: 'application/n-quads'});
216216

217217
// deserialize from RDF
218-
const doc = await jsonld.fromRDF(nquads, {format: 'application/nquads'});
218+
const doc = await jsonld.fromRDF(nquads, {format: 'application/n-quads'});
219219

220220
// register a custom promise-based RDF parser
221221
jsonld.registerRDFParser(contentType, async input => {

lib/jsonld.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,9 @@ jsonld.link = util.callbackify(async function(input, ctx, options) {
500500
* [base] the base IRI to use.
501501
* [expandContext] a context to expand with.
502502
* [inputFormat] the format if input is not JSON-LD:
503-
* 'application/nquads' for N-Quads.
503+
* 'application/n-quads' for N-Quads.
504504
* [format] the format if output is a string:
505-
* 'application/nquads' for N-Quads.
505+
* 'application/n-quads' for N-Quads.
506506
* [documentLoader(url, callback(err, remoteDoc))] the document loader.
507507
* @param [callback(err, normalized)] called once the operation completes.
508508
*
@@ -520,7 +520,8 @@ jsonld.normalize = jsonld.canonize = util.callbackify(async function(
520520
algorithm: 'URDNA2015'
521521
});
522522
if('inputFormat' in options) {
523-
if(options.inputFormat !== 'application/nquads') {
523+
if(options.inputFormat !== 'application/n-quads' &&
524+
options.inputFormat !== 'application/nquads') {
524525
throw new JsonLdError(
525526
'Unknown canonicalization input format.',
526527
'jsonld.CanonizeError');
@@ -549,7 +550,7 @@ jsonld.normalize = jsonld.canonize = util.callbackify(async function(
549550
* format option or an RDF dataset to convert.
550551
* @param [options] the options to use:
551552
* [format] the format if dataset param must first be parsed:
552-
* 'application/nquads' for N-Quads (default).
553+
* 'application/n-quads' for N-Quads (default).
553554
* [rdfParser] a custom RDF-parser to use to parse the dataset.
554555
* [useRdfType] true to use rdf:type, false to use @type
555556
* (default: false).
@@ -566,7 +567,7 @@ jsonld.fromRDF = util.callbackify(async function(dataset, options) {
566567

567568
// set default options
568569
options = _setDefaults(options, {
569-
format: _isString(dataset) ? 'application/nquads' : undefined
570+
format: _isString(dataset) ? 'application/n-quads' : undefined
570571
});
571572

572573
let {format, rdfParser} = options;
@@ -622,7 +623,7 @@ jsonld.fromRDF = util.callbackify(async function(dataset, options) {
622623
* [base] the base IRI to use.
623624
* [expandContext] a context to expand with.
624625
* [format] the format to use to output a string:
625-
* 'application/nquads' for N-Quads.
626+
* 'application/n-quads' for N-Quads.
626627
* [produceGeneralizedRdf] true to output generalized RDF, false
627628
* to produce only standard RDF (default: false).
628629
* [documentLoader(url, callback(err, remoteDoc))] the document loader.
@@ -648,7 +649,8 @@ jsonld.toRDF = util.callbackify(async function(input, options) {
648649
// output RDF dataset
649650
const dataset = _toRDF(expanded, options);
650651
if(options.format) {
651-
if(options.format === 'application/nquads') {
652+
if(options.format === 'application/n-quads' ||
653+
options.format === 'application/nquads') {
652654
return await NQuads.serialize(dataset);
653655
}
654656
throw new JsonLdError(
@@ -974,6 +976,7 @@ jsonld.unregisterRDFParser = function(contentType) {
974976
};
975977

976978
// register the N-Quads RDF parser
979+
jsonld.registerRDFParser('application/n-quads', NQuads.parse);
977980
jsonld.registerRDFParser('application/nquads', NQuads.parse);
978981

979982
// register the RDFa API RDF parser

tests/misc.js

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,21 @@ describe('other toRDF tests', () => {
117117
});
118118
});
119119

120-
it('should handle nquads format', done => {
120+
it('should handle N-Quads format', done => {
121+
const doc = {
122+
'@id': 'https://example.com/',
123+
'https://example.com/test': 'test'
124+
};
125+
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, output) => {
126+
assert.ifError(err);
127+
assert.equal(
128+
output,
129+
'<https://example.com/> <https://example.com/test> "test" .\n');
130+
done();
131+
});
132+
});
133+
134+
it('should handle deprecated N-Quads format', done => {
121135
const doc = {
122136
'@id': 'https://example.com/',
123137
'https://example.com/test': 'test'
@@ -132,6 +146,119 @@ describe('other toRDF tests', () => {
132146
});
133147
});
134148

149+
describe('other fromRDF tests', () => {
150+
const emptyNQuads = '';
151+
const emptyRdf = [];
152+
153+
it('should process with options and callback', done => {
154+
jsonld.fromRDF('', {}, (err, output) => {
155+
assert.ifError(err);
156+
assert.deepEqual(output, emptyRdf);
157+
done();
158+
});
159+
});
160+
161+
it('should process with no options and callback', done => {
162+
jsonld.fromRDF(emptyNQuads, (err, output) => {
163+
assert.ifError(err);
164+
assert.deepEqual(output, emptyRdf);
165+
done();
166+
});
167+
});
168+
169+
it('should process with options and promise', done => {
170+
const p = jsonld.fromRDF(emptyNQuads, {});
171+
assert(p instanceof Promise);
172+
p.catch(e => {
173+
assert.fail();
174+
}).then(output => {
175+
assert.deepEqual(output, emptyRdf);
176+
done();
177+
});
178+
});
179+
180+
it('should process with no options and promise', done => {
181+
const p = jsonld.fromRDF(emptyNQuads);
182+
assert(p instanceof Promise);
183+
p.catch(e => {
184+
assert.fail();
185+
}).then(output => {
186+
assert.deepEqual(output, emptyRdf);
187+
done();
188+
});
189+
});
190+
191+
it('should fail with no args and callback', done => {
192+
jsonld.fromRDF((err, output) => {
193+
assert(err);
194+
done();
195+
});
196+
});
197+
198+
it('should fail with no args and promise', done => {
199+
const p = jsonld.fromRDF();
200+
assert(p instanceof Promise);
201+
p.then(output => {
202+
assert.fail();
203+
}).catch(e => {
204+
assert(e);
205+
done();
206+
})
207+
});
208+
209+
it('should fail for bad format and callback', done => {
210+
jsonld.fromRDF(emptyNQuads, {format: 'bogus'}, (err, output) => {
211+
assert(err);
212+
assert.equal(err.name, 'jsonld.UnknownFormat');
213+
done();
214+
});
215+
});
216+
217+
it('should fail for bad format and promise', done => {
218+
const p = jsonld.fromRDF(emptyNQuads, {format: 'bogus'});
219+
assert(p instanceof Promise);
220+
p.then(() => {
221+
assert.fail();
222+
}).catch(e => {
223+
assert(e);
224+
assert.equal(e.name, 'jsonld.UnknownFormat');
225+
done();
226+
});
227+
});
228+
229+
it('should handle N-Quads format', done => {
230+
const nq = '<https://example.com/> <https://example.com/test> "test" .\n';
231+
jsonld.fromRDF(nq, {format: 'application/n-quads'}, (err, output) => {
232+
assert.ifError(err);
233+
assert.deepEqual(
234+
output,
235+
[{
236+
'@id': 'https://example.com/',
237+
'https://example.com/test': [{
238+
'@value': 'test'
239+
}]
240+
}]);
241+
done();
242+
});
243+
});
244+
245+
it('should handle deprecated N-Quads format', done => {
246+
const nq = '<https://example.com/> <https://example.com/test> "test" .\n';
247+
jsonld.fromRDF(nq, {format: 'application/nquads'}, (err, output) => {
248+
assert.ifError(err);
249+
assert.deepEqual(
250+
output,
251+
[{
252+
'@id': 'https://example.com/',
253+
'https://example.com/test': [{
254+
'@value': 'test'
255+
}]
256+
}]);
257+
done();
258+
});
259+
});
260+
});
261+
135262
describe('loading multiple levels of contexts', () => {
136263
const documentLoader = url => {
137264
if(url === 'https://example.com/context1') {

tests/test-common.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ const TEST_TYPES = {
7878
fn: 'fromRDF',
7979
params: [
8080
readTestNQuads('input'),
81-
createTestOptions({format: 'application/nquads'})
81+
createTestOptions({format: 'application/n-quads'})
8282
],
8383
compare: compareExpectedJson
8484
},
8585
'jld:NormalizeTest': {
8686
fn: 'normalize',
8787
params: [
8888
readTestUrl('input'),
89-
createTestOptions({format: 'application/nquads'})
89+
createTestOptions({format: 'application/n-quads'})
9090
],
9191
compare: compareExpectedNQuads
9292
},
@@ -95,7 +95,7 @@ const TEST_TYPES = {
9595
fn: 'toRDF',
9696
params: [
9797
readTestUrl('input'),
98-
createTestOptions({format: 'application/nquads'})
98+
createTestOptions({format: 'application/n-quads'})
9999
],
100100
compare: compareExpectedNQuads
101101
},
@@ -105,8 +105,8 @@ const TEST_TYPES = {
105105
readTestNQuads('action'),
106106
createTestOptions({
107107
algorithm: 'URGNA2012',
108-
inputFormat: 'application/nquads',
109-
format: 'application/nquads'
108+
inputFormat: 'application/n-quads',
109+
format: 'application/n-quads'
110110
})
111111
],
112112
compare: compareExpectedNQuads
@@ -117,8 +117,8 @@ const TEST_TYPES = {
117117
readTestNQuads('action'),
118118
createTestOptions({
119119
algorithm: 'URDNA2015',
120-
inputFormat: 'application/nquads',
121-
format: 'application/nquads'
120+
inputFormat: 'application/n-quads',
121+
format: 'application/n-quads'
122122
})
123123
],
124124
compare: compareExpectedNQuads

0 commit comments

Comments
 (0)