C Collections

cyaml · Section 3

cyaml_serialize_stream

cyaml_serialize_stream - serialize a list of documents as a multi-document YAML stream

C Collections (libccollections, -lccollections)

#include <ccollections/cyaml.h>
 
char *cyaml_serialize_stream(cyaml node);

Writes a list of documents back as a YAML stream, which is the
opposite of parsing one.  For an input that holds more than one
document, cyaml_parse(3) gives a CYAML_LIST with one element per
document, and this function writes each element of such a list as a
document of its own.  Each document starts with a "---" line followed
by the block-style YAML that cyaml_serialize(3) writes for the
element, so parsing the result for a list of two or more documents
gives back an equal list.  A stream of Kubernetes manifests, for
example, can be parsed, edited and written back for a tool that
reads it one document at a time.
 
An empty list gives an empty stream, the empty string "".  Parsing an
empty string gives a CYAML_NULL and not an empty list, because a
stream with no documents reads the same as an empty document.
 
A node that is not a list, a NULL node included, is written as a
stream of one document, so the root of a one-document parse writes
back as that same document.  A list that is the root of a single
document cannot be told apart from a stream of documents, neither in
the parse nor here, so write such a document with cyaml_serialize(3).
 
The list's own tag is not written, because a stream has no node that
could carry it, and a list that comes from parsing a stream never has
one.
 
Every document stands alone, and the output has no directive and no
anchor.  A tag is written in the same form as in cyaml_serialize(3),
which needs no %TAG directive, and an alias of the parsed input is an
independent copy in the tree anyway.  Scalars, dictionary keys,
floats, the member order of a dictionary and the locale all follow
the rules of cyaml_serialize(3).  Its nesting limit of 500 nested
containers counts the list too, exactly as parsing a stream of
several documents does.  So each element of a list may nest at most
499 containers, which keeps every stream this function writes
parseable, and a node that is not a list may nest 500.
 
The returned string comes from node's allocator.  Free it with
cyaml_serialize_free() when the tree used the default allocator, and
with cyaml_serialize_free_mp() and that same allocator when it used a
custom one.

On success, a null-terminated string on the heap.  NULL when the
function runs out of memory or node nests more than 500 containers,
counting the list.

char *err = NULL;
cyaml docs = cyaml_parse(stream, &err);   /* three manifests */
if (!docs) {
    fprintf(stderr, "parse failed: %s\n", err);
    return -1;
}
cyaml_set(docs, "#1.spec.replicas", 5);
char *out = cyaml_serialize_stream(docs);
if (out)   /* "---\napiVersion: v1\n...\n---\napiVersion: apps/v1\n..." */
    fputs(out, stdout);
cyaml_serialize_free(out);
cyaml_destroy(docs);

cyaml_parse(3), cyaml_serialize(3), cyaml_serialize_flow(3), cyaml_serialize_free(3), cyaml_serialize_free_mp(3)