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

adds support for writing hextuples, fixes #44. #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions include/serd/serd.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef enum {
SERD_NTRIPLES = 2U, ///< Line-based triples http://www.w3.org/TR/n-triples/
SERD_NQUADS = 3U, ///< Line-based quads http://www.w3.org/TR/n-quads/
SERD_TRIG = 4U, ///< Terse quads http://www.w3.org/TR/trig/
SERD_HEXTUPLES = 5U, ///< ndjson hex tuples https://github.com/ontola/hextuples
} SerdSyntax;

/// Flags indicating certain string properties relevant to serialisation
Expand Down
102 changes: 91 additions & 11 deletions src/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ write_node(SerdWriter* writer,
SERD_NODISCARD static bool
supports_abbrev(const SerdWriter* writer)
{
return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG;
return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG || writer->syntax == SERD_HEXTUPLES;
}

static SerdStatus
Expand Down Expand Up @@ -554,9 +554,9 @@ write_sep(SerdWriter* writer, const Sep sep)

// Write newline or space before separator if necessary
if (pre_line) {
TRY(st, write_newline(writer));
TRY(st, write_newline(writer));
} else if (rule->pre_space_after & (1U << writer->last_sep)) {
TRY(st, esink(" ", 1, writer));
TRY(st, esink(" ", 1, writer));
}

// Write actual separator string
Expand Down Expand Up @@ -704,20 +704,37 @@ write_uri_node(SerdWriter* const writer,
if (supports_abbrev(writer)) {
if (field == FIELD_PREDICATE &&
!strcmp((const char*)node->buf, NS_RDF "type")) {
return esink("a", 1, writer);
if (writer->syntax == SERD_HEXTUPLES) {
return esink("\"a\"", 3, writer);
}
else {
return esink("a", 1, writer);
}
}

if (!strcmp((const char*)node->buf, NS_RDF "nil")) {
return esink("()", 2, writer);
if (writer->syntax == SERD_HEXTUPLES) {
Copy link
Owner

Choose a reason for hiding this comment

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

This is starting to push the limits of what makes sense in a single writer. It's probably still (barely) fine, but probably time to split this thing up by syntax.

return esink("null", 4, writer);
}
else {
return esink("()", 2, writer);
}
}

if (has_scheme && (writer->style & SERD_STYLE_CURIED) &&
serd_env_qualify(writer->env, node, &prefix, &suffix) &&
is_name(prefix.buf, prefix.n_bytes) &&
is_name(suffix.buf, suffix.len)) {
if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("\"", 1, writer));
}
TRY(st, write_uri_from_node(writer, &prefix));
TRY(st, esink(":", 1, writer));
return ewrite_uri(writer, suffix.buf, suffix.len);
TRY(st, ewrite_uri(writer, suffix.buf, suffix.len));
if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("\"", 1, writer));
}
return st;
}
}

Expand All @@ -730,7 +747,11 @@ write_uri_node(SerdWriter* const writer,
node->buf);
}

TRY(st, esink("<", 1, writer));
if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("\"", 1, writer));
} else {
TRY(st, esink("<", 1, writer));
}

if (writer->style & SERD_STYLE_RESOLVED) {
SerdURI in_base_uri;
Expand All @@ -745,7 +766,7 @@ write_uri_node(SerdWriter* const writer,
SerdURI* root = rooted ? &writer->root_uri : &writer->base_uri;
UriSinkContext ctx = {writer, SERD_SUCCESS};
if (!uri_is_under(&abs_uri, root) || writer->syntax == SERD_NTRIPLES ||
writer->syntax == SERD_NQUADS) {
writer->syntax == SERD_NQUADS || writer->syntax == SERD_HEXTUPLES) {
serd_uri_serialise(&abs_uri, uri_sink, &ctx);
} else {
serd_uri_serialise_relative(
Expand All @@ -755,7 +776,18 @@ write_uri_node(SerdWriter* const writer,
TRY(st, write_uri_from_node(writer, node));
}

return esink(">", 1, writer);
if (writer->syntax == SERD_HEXTUPLES) {
/* TRY(st, esink("$CUE", 4, writer)); */
if (field == FIELD_PREDICATE || field == FIELD_SUBJECT ){
return esink("\"", 1, writer);
} else {
TRY(st, esink("\"", 1, writer));
TRY(st, write_sep(writer, SEP_LIST_END));
return st;
}
} else {
return esink(">", 1, writer);
}
}

SERD_NODISCARD static SerdStatus
Expand Down Expand Up @@ -812,6 +844,10 @@ write_blank(SerdWriter* const writer,
}
}

if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("\"", 1, writer));
}

TRY(st, esink("_:", 2, writer));
if (writer->bprefix && !strncmp((const char*)node->buf,
(const char*)writer->bprefix,
Expand All @@ -823,6 +859,9 @@ write_blank(SerdWriter* const writer,
} else {
TRY(st, esink(node->buf, node->n_bytes, writer));
}
if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("\"", 1, writer));
}

return st;
}
Expand Down Expand Up @@ -946,6 +985,42 @@ serd_writer_write_statement(SerdWriter* writer,
TRY(st, esink(" .\n", 3, writer));
return SERD_SUCCESS;
}
else if (writer->syntax == SERD_HEXTUPLES) {
TRY(st, esink("[", 1, writer));
TRY(st, write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags));
TRY(st, esink(", ", 2, writer));
TRY(st, write_node(writer, predicate, NULL, NULL, FIELD_PREDICATE, flags));
TRY(st, esink(", ", 2, writer));
// object
TRY(st, esink("\"", 1, writer));
TRY(st, write_text(writer, WRITE_STRING, object->buf, object->n_bytes));
st = esink("\"", 1, writer);

TRY(st, esink(", ", 2, writer));
//datatype
if (datatype && datatype->buf) {
st = write_node(writer, datatype, NULL, NULL, FIELD_NONE, flags);
} else {
TRY(st, esink("\"\"", 2, writer));
}
TRY(st, esink(", ", 2, writer));
// lang
TRY(st, esink("\"", 1, writer));
if (lang && lang->buf) {
st = esink(lang->buf, lang->n_bytes, writer);
}
TRY(st, esink("\"", 1, writer));

TRY(st, esink(", ", 2, writer));
TRY(st, esink("\"", 1, writer));
if (graph) {
TRY(st, write_node(writer, graph, datatype, lang, FIELD_GRAPH, flags));
}
TRY(st, esink("\"", 1, writer));
/* TRY(st, esink("]\n", 3, writer)); */
TRY(st, esink("]\n", 2, writer));
return SERD_SUCCESS;
}

SERD_DISABLE_NULL_WARNINGS

Expand All @@ -968,7 +1043,12 @@ serd_writer_write_statement(SerdWriter* writer,
// Continue a list
if (!strcmp((const char*)predicate->buf, NS_RDF "first") &&
!strcmp((const char*)object->buf, NS_RDF "nil")) {
return esink("()", 2, writer);

if (writer->syntax == SERD_HEXTUPLES) {
return esink("null", 4, writer);
} else {
return esink("()", 2, writer);
}
}

TRY_FAILING(
Expand Down Expand Up @@ -1070,7 +1150,7 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node)

SerdStatus st = SERD_SUCCESS;

if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) {
if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS || writer->syntax == SERD_HEXTUPLES) {
return SERD_SUCCESS;
}

Expand Down