# 2.9 Writer

typedef struct SerdWriterImpl SerdWriter

  Streaming serialiser that writes a text stream as statements are pushed.

enum SerdStyle

  Syntax style options.

  These flags allow more precise control of writer output style. Note that some options are only supported for some syntaxes, for example, NTriples does not support abbreviation and is always ASCII.

  enumerator SERD_STYLE_ABBREVIATED

    Abbreviate triples when possible.

  enumerator SERD_STYLE_ASCII

    Escape all non-ASCII characters.

  enumerator SERD_STYLE_RESOLVED

    Resolve URIs against base URI.

  enumerator SERD_STYLE_CURIED

    Shorten URIs into CURIEs.

  enumerator SERD_STYLE_BULK

    Write output in pages.

  enumerator SERD_STYLE_STRICT

    Abort with error on lossy output.

size_t serd_chunk_sink(const void *buf, size_t len, void *stream)

  A convenience sink function for writing to a string.

  This function can be used as a `SerdSink` to write to a `SerdChunk` which is resized as necessary with realloc(). The `stream` parameter must point to an initialized `SerdChunk`. When the write is finished, the string should be retrieved with `serd_chunk_sink_finish()`.

uint8_t *serd_chunk_sink_finish(SerdChunk *stream)

  Finish a serialisation to a chunk with `serd_chunk_sink()`.

  The returned string is the result of the serialisation, which is null terminated (by this function) and owned by the caller.

size_t serd_file_sink(const void *buf, size_t len, void *stream)

  A convenience sink function for writing to a FILE*.

  This function can be used as a `SerdSink` when writing to a FILE*. The `stream` parameter must be a FILE* opened for writing.

void serd_writer_chop_blank_prefix(SerdWriter *writer, const uint8_t *prefix)

  Set a prefix to be removed from matching blank node identifiers.

  This is the counterpart to `serd_reader_add_blank_prefix()` which can be used to “undo” added prefixes.

SerdStatus serd_writer_end_anon(SerdWriter *writer, const SerdNode *node)

  Mark the end of an anonymous node’s description.

  Note this function can be safely casted to `SerdEndSink`.

SerdStatus serd_writer_finish(SerdWriter *writer)

  Finish a write.

  This flushes any pending output, for example terminating punctuation, so that the output is a complete document.

void serd_writer_free(SerdWriter *writer)

  Free `writer`.

SerdEnv *serd_writer_get_env(SerdWriter *writer)

  Return the env used by `writer`.

SerdWriter *serd_writer_new(SerdSyntax syntax, SerdStyle style, SerdEnv *env, const SerdURI *base_uri, SerdSink ssink, void *stream)

  Create a new RDF writer.

SerdStatus serd_writer_set_base_uri(SerdWriter *writer, const SerdNode *uri)

  Set the current output base URI, and emit a directive if applicable.

  Note this function can be safely casted to `SerdBaseSink`.

void serd_writer_set_error_sink(SerdWriter *writer, SerdErrorSink error_sink, void *error_handle)

  Set a function to be called when errors occur during writing.

  The `error_sink` will be called with `handle` as its first argument. If no error function is set, errors are printed to stderr.

SerdStatus serd_writer_set_prefix(SerdWriter *writer, const SerdNode *name, const SerdNode *uri)

  Set a namespace prefix (and emit directive if applicable).

  Note this function can be safely casted to `SerdPrefixSink`.

SerdStatus serd_writer_set_root_uri(SerdWriter *writer, const SerdNode *uri)

  Set the current root URI.

  The root URI should be a prefix of the base URI. The path of the root URI is the highest path any relative up-reference can refer to. For example, with root file:///foo/root and base file:///foo/root/base, file:///foo/root will be written as <../>, but file:///foo will be written non-relatively as file:///foo. If the root is not explicitly set, it defaults to the base URI, so no up-references will be created at all.

SerdStatus serd_writer_write_statement(SerdWriter *writer, SerdStatementFlags flags, const SerdNode *graph, const SerdNode *subject, const SerdNode *predicate, const SerdNode *object, const SerdNode *datatype, const SerdNode *lang)

  Write a statement.

  Note this function can be safely casted to `SerdStatementSink`.