Skip to content
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

Serialize subject and predicate literals correctly #330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
38 changes: 16 additions & 22 deletions src/N3Writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ export default class N3Writer {
if (subject.equals(this._subject)) {
// Don't repeat the predicate if it's the same
if (predicate.equals(this._predicate))
this._write(`, ${this._encodeObject(object)}`, done);
this._write(`, ${this._encodeTerm(object)}`, done);
// Same subject, different predicate
else
this._write(`;\n ${
this._encodePredicate(this._predicate = predicate)} ${
this._encodeObject(object)}`, done);
this._encodeTerm(object)}`, done);
}
// Different subject; write the whole quad
else
this._write(`${(this._subject === null ? '' : '.\n') +
this._encodeSubject(this._subject = subject)} ${
this._encodeTerm(this._subject = subject)} ${
this._encodePredicate(this._predicate = predicate)} ${
this._encodeObject(object)}`, done);
this._encodeTerm(object)}`, done);
}
catch (error) { done && done(error); }
}
Expand All @@ -122,9 +122,9 @@ export default class N3Writer {

// ### `quadToString` serializes a quad as a string
quadToString(subject, predicate, object, graph) {
return `${this._encodeSubject(subject)} ${
this._encodeIriOrBlank(predicate)} ${
this._encodeObject(object)
return `${this._encodeTerm(subject)} ${
this._encodeTerm(predicate)} ${
this._encodeTerm(object)
}${graph && graph.value ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
}

Expand All @@ -135,12 +135,6 @@ export default class N3Writer {
}).join('');
}

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

// ### `_encodeIriOrBlank` represents an IRI or blank node
_encodeIriOrBlank(entity) {
// A blank node or list is represented as-is
Expand Down Expand Up @@ -210,11 +204,11 @@ 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) {
// ### `_encodeTerm` represents a term
_encodeTerm(object) {
switch (object.termType) {
case 'Quad':
return this._encodeQuad(object);
Expand All @@ -228,9 +222,9 @@ export default class N3Writer {
// ### `_encodeQuad` encodes an RDF* quad
_encodeQuad({ subject, predicate, object, graph }) {
return `<<${
this._encodeSubject(subject)} ${
this._encodeTerm(subject)} ${
this._encodePredicate(predicate)} ${
this._encodeObject(object)}${
this._encodeTerm(object)}${
isDefaultGraph(graph) ? '' : ` ${this._encodeIriOrBlank(graph)}`}>>`;
}

Expand Down Expand Up @@ -324,7 +318,7 @@ export default class N3Writer {
child = children[0];
if (!(child.object instanceof SerializedTerm))
return new SerializedTerm(`[ ${this._encodePredicate(child.predicate)} ${
this._encodeObject(child.object)} ]`);
this._encodeTerm(child.object)} ]`);
// Generate a multi-triple or nested blank node
default:
let contents = '[';
Expand All @@ -333,12 +327,12 @@ export default class N3Writer {
child = children[i];
// Write only the object is the predicate is the same as the previous
if (child.predicate.equals(predicate))
contents += `, ${this._encodeObject(child.object)}`;
contents += `, ${this._encodeTerm(child.object)}`;
// Otherwise, write the predicate and the object
else {
contents += `${(i ? ';\n ' : '\n ') +
this._encodePredicate(child.predicate)} ${
this._encodeObject(child.object)}`;
this._encodeTerm(child.object)}`;
predicate = child.predicate;
}
}
Expand All @@ -350,7 +344,7 @@ export default class N3Writer {
list(elements) {
const length = elements && elements.length || 0, contents = new Array(length);
for (let i = 0; i < length; i++)
contents[i] = this._encodeObject(elements[i]);
contents[i] = this._encodeTerm(elements[i]);
return new SerializedTerm(`(${contents.join(' ')})`);
}

Expand Down
6 changes: 6 additions & 0 deletions test/N3Writer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ describe('Writer', () => {
const writer = new Writer();
writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g'))).should.equal('<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`));
});
});

Expand Down