From c7540c4c5cd4fe40447dd90a683e1ea878e75519 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Thu, 2 Mar 2023 10:17:28 +0100 Subject: [PATCH] Serialize subject and predicate literals correctly --- src/N3Writer.js | 38 ++++++++++++++++---------------------- test/N3Writer-test.js | 6 ++++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/N3Writer.js b/src/N3Writer.js index abc3852f..d35e5d63 100644 --- a/src/N3Writer.js +++ b/src/N3Writer.js @@ -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); } } @@ -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'}`; } @@ -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 @@ -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); @@ -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)}`}>>`; } @@ -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 = '['; @@ -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; } } @@ -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(' ')})`); } diff --git a/test/N3Writer-test.js b/test/N3Writer-test.js index 33660145..a46aabaf 100644 --- a/test/N3Writer-test.js +++ b/test/N3Writer-test.js @@ -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(' << >> .\n'); }); + + it('should serialize a triple with a literal as subject', + shouldSerialize([`"123"^^${xsd.boolean}`, 'b', 'c'], `"123"^^<${xsd.boolean}> .\n`)); + + it('should serialize a triple with a literal as predicate', + shouldSerialize(['a', `"123"^^${xsd.boolean}`, 'c'], ` "123"^^<${xsd.boolean}> .\n`)); }); });