Skip to content

Serialize subject and predicate literals correctly - #330

Open
joachimvh wants to merge 4 commits into
rdfjs:mainfrom
joachimvh:subject-literals
Open

Serialize subject and predicate literals correctly#330
joachimvh wants to merge 4 commits into
rdfjs:mainfrom
joachimvh:subject-literals

Conversation

@joachimvh

Copy link
Copy Markdown

Closes #329

This does have as disadvantage that invalid turtle will be generated if quads get added to the writer that can not be represented in turtle. If that is a problem, an alternative solution would be to have variants of the writer, similarly to how n-quads get handled, where the turtle version throws an error if it encounters a literal in the subject/predicate position and the N3 version handles it correctly. Let me know what you prefer and I can update the PR.

@RubenVerborgh

RubenVerborgh commented Mar 24, 2023

Copy link
Copy Markdown
Contributor

invalid turtle will be generated if quads get added to the writer that can not be represented in turtle

I wonder if this has repercussions for #339; should we have a generic flag for "out of syntax features"?

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps we should still keep _encodeSubject and _encodeObject around, just so we can error in those functions as needed.

@jeswr

jeswr commented Mar 24, 2023

Copy link
Copy Markdown
Member

I wonder if this has repercussions for #339; should we have a generic flag for "out of syntax features"?

...perhaps, though I worry about performance degradation doing it internally; we could just supply validTurtleQuad, validN3Quad etc. functions that consumers can optionally pass their data through first to validate.

@jeswr

jeswr commented Mar 24, 2023

Copy link
Copy Markdown
Member

As an aside - are there any RDF syntaxes that don't support a shortand in the subject or object position? If not then the following logic could just be moved to _encodeTerm

return predicate.value === rdf.type ? 'a' : this._encodeTerm(predicate);

@joachimvh

Copy link
Copy Markdown
Author

As an aside - are there any RDF syntaxes that don't support a shortand in the subject or object position?

Turtle only allows it in the predicate position.

jeswr and others added 2 commits July 2, 2026 20:44
Resolves conflicts with the RDF 1.2 triple-term syntax; keeps
_encodeSubject/_encodeObject as thin wrappers around the shared
literal-aware _encodeTerm encoding.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…mats

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@jeswr

jeswr commented Jul 2, 2026

Copy link
Copy Markdown
Member

Per @jeswr, I've pushed directly to this branch (thanks to "allow edits by maintainers") to bring it up to date — @joachimvh's diagnosis and fix are untouched and remain the core of this PR; his original commit is preserved as-is.

What was added on top:

  • Merged current main into the branch (merge, not rebase). Conflicts were limited to _encodeQuad (now RDF 1.2 <<(...)>> triple-term syntax) and the writer tests.
  • Per @RubenVerborgh's earlier suggestion, _encodeSubject and _encodeObject are kept as thin wrappers around the shared literal-aware _encodeTerm, so they remain entry points for format-dependent errors if we want them later.
  • quadToString predicates now also go through the literal-aware encoding.
  • Additional tests: datatyped and language-tagged literal subjects, literal predicates, text/n3 output, quadToString variants, and an N3 round-trip.

npm run lint and npm test pass with 100% coverage. Should be ready for @jeswr's re-review.

@TallTed

TallTed commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

@jeswr commented ... "Per @jeswr, I've pushed ... Should be ready for @jeswr's re-review."

How many @jeswr are there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serializing literal subjects returns invalid results

4 participants