Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 12 additions & 8 deletions src/N3Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class N3Writer {
// ### `quadToString` serializes a quad as a string
quadToString(subject, predicate, object, graph) {
return `${this._encodeSubject(subject)} ${
this._encodeIriOrBlank(predicate)} ${
this._encodeTerm(predicate)} ${
this._encodeObject(object)
}${graph && !isDefaultGraph(graph) ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
}
Expand All @@ -140,8 +140,7 @@ export default class N3Writer {

// ### `_encodeSubject` represents a subject
_encodeSubject(entity) {
return entity.termType === 'Quad' ?
this._encodeQuad(entity) : this._encodeIriOrBlank(entity);
return this._encodeTerm(entity);
}

// ### `_encodeIriOrBlank` represents an IRI or blank node
Expand Down Expand Up @@ -215,18 +214,23 @@ export default class N3Writer {

// ### `_encodePredicate` represents a predicate
_encodePredicate(predicate) {
return predicate.value === rdf.type ? 'a' : this._encodeIriOrBlank(predicate);
return predicate.value === rdf.type ? 'a' : this._encodeTerm(predicate);
}

// ### `_encodeObject` represents an object
_encodeObject(object) {
switch (object.termType) {
return this._encodeTerm(object);
}

// ### `_encodeTerm` represents an arbitrary term
_encodeTerm(term) {
switch (term.termType) {
case 'Quad':
return this._encodeQuad(object);
return this._encodeQuad(term);
case 'Literal':
return this._encodeLiteral(object);
return this._encodeLiteral(term);
default:
return this._encodeIriOrBlank(object);
return this._encodeIriOrBlank(term);
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/N3Writer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,45 @@ describe('Writer', () => {
).toBe('<a> <b> <<(<a> <b> <c> <g>)>> .\n');
});

it('should serialize a triple with a literal as subject',
shouldSerialize([`"123"^^${xsd.boolean}`, 'b', 'c'], `"123"^^<${xsd.boolean}> <b> <c>.\n`));

it('should serialize a triple with a literal as predicate',
shouldSerialize(['a', `"123"^^${xsd.boolean}`, 'c'], `<a> "123"^^<${xsd.boolean}> <c>.\n`));

it('should serialize a triple with a language-tagged literal as subject',
shouldSerialize(['"hello"@en-us', 'b', 'c'], '"hello"@en-us <b> <c>.\n'));

it('should serialize a triple with a literal as subject in N3 mode',
shouldSerialize({ format: 'text/n3' },
[`"123"^^${xsd.boolean}`, 'b', 'c'],
`"123"^^<${xsd.boolean}> <b> <c>.\n`));

it('should serialize a triple with a literal as subject via quadToString', () => {
const writer = new Writer();
expect(
writer.quadToString(termFromId(`"123"^^${xsd.boolean}`), new NamedNode('b'), new NamedNode('c')),
).toBe(`"123"^^<${xsd.boolean}> <b> <c> .\n`);
});

it('should serialize a triple with a literal as predicate via quadToString', () => {
const writer = new Writer();
expect(
writer.quadToString(new NamedNode('a'), termFromId(`"123"^^${xsd.boolean}`), new NamedNode('c')),
).toBe(`<a> "123"^^<${xsd.boolean}> <c> .\n`);
});

it('should round-trip a triple with a literal as subject in N3 mode', done => {
const quad = new Quad(termFromId(`"1"^^${xsd.boolean}`),
new NamedNode('http://example.com/p'), new NamedNode('http://example.com/o'));
const writer = new Writer({ format: 'text/n3' });
writer.addQuad(quad);
writer.end((error, output) => {
expect(new Parser({ format: 'text/n3' }).parse(output)).toEqual([quad]);
done(error);
});
});

/*
* Test relativization of IRIs
*
Expand Down