C Collections

cyaml · Section 3

cyaml_serialize

cyaml_serialize - serialize a DOM tree to block-style YAML

C Collections (libccollections, -lccollections)

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

Writes the DOM subtree that node names as a readable, block-style
YAML document.  A scalar is written as plain text when that is safe,
meaning that it makes the round trip with no doubt about its type,
and double-quoted in every other case.  A scalar whose first
character is U+FEFF is double-quoted too, because at offset 0 of a
document those bytes are a byte order mark and not content, and the
parser removes them.  A scalar is written plain only when a YAML 1.1
reader such as PyYAML or libyaml would read it as the same string as
well.  So a string that holds a tab, a '?', or one of the YAML 1.1
line breaks NEL (U+0085), LS (U+2028) and PS (U+2029), and the string
"=", are double-quoted, and those three line breaks are written as
their \N, \L and \P escapes, which every reader takes as the same
code point.  A string that holds a C1 control character (U+0080 to
U+009F, other than NEL) or one of the noncharacters U+FFFE and U+FFFF
is double-quoted as well, because YAML 1.2 doesn't allow those code
points raw and libyaml and PyYAML refuse a document that holds one.
A C1 control is written as \xNN and the two noncharacters as \uFFFE
and \uFFFF.  A dictionary key follows the same rules.  A list uses
the "- value" block notation, a dictionary the "key: value" block
notation, and each nesting level adds two spaces of indentation.
 
Dictionary members come out in insertion order, which is the order of
the parsed text or of the cyaml_dictionary_set(3) and cyaml_set(3)
calls that added each key.  A key whose value is replaced keeps its
place and a removed key leaves the order, so parsing and then
serializing keeps the key order of the source document, the same in
every run.  cyaml_dictionary_first(3) walks the same order.  List
elements always come out in exactly the order in which the caller
pushed them or the parser read them.
 
A float is written with the shortest of 15, 16 or 17 significant
digits that reads back as the same double (for a subnormal double,
which has fewer significant bits, the shortest of 1 to 17 digits),
and its mantissa always has a '.': "1.0", "0.25", "1.0e+20",
"1.0e-05", "5.0e-324".  Without the '.', "1" would read back as an
integer, and "1e+20" as a string under YAML 1.1, whose float grammar
requires the '.'.  ".inf", "-.inf" and ".nan" keep their spelling.
 
A dictionary key that is exactly the canonical text of an integer or
float key, such as "16", "3.1" or ".inf", is written without quotes,
so it keeps its numeric type and parsing the output canonicalizes it
back to the same key (see cyaml_parse(3)).  A key that a parse would
read as another type or another text, such as "010", "3.10" or
"true", is double-quoted.
 
A key whose rendered text is longer than 1024 bytes is written in the
explicit form, with "? key" on one line and ": value" at the same
indentation on the next.  YAML 1.2 limits an implicit key to 1024
characters, and PyYAML and libyaml refuse a longer one.
cyaml_parse(3) reads the explicit form back as the same entry.
 
The decimal point of a float is always '.', whatever LC_NUMERIC the
process or the calling thread uses.  For the length of the call the
calling thread switches to the "C" locale through uselocale(3), which
changes no other thread, and the thread's own locale is back in place
before the call returns.
 
A node's custom tag (see cyaml_node_tag(3)) is written in front of it
as a "!<...>" verbatim tag.  A core-schema tag (CYAML_TAG_NULL, _BOOL,
_INT, _FLOAT, _STR, _SEQ or _MAP) is never written.  A node only ever
carries the core-schema tag of its own type (see
cyaml_node_set_tag(3)), and the quoting and formatting rules around
the value already make the value come back as that same type without
the tag.
 
The text of a tag always survives the round trip in full.  Every byte
that the URI characters of YAML 1.2 section 5.6 don't allow literally
inside "!<...>" is written as a percent escape: The '%' that starts an
escape, a '>', a line break, a space, a quote, a brace, every control
byte and every byte of a UTF-8 sequence, among others.  A '#' is
written as %23 as well, because libyaml and PyYAML refuse a verbatim
tag that holds one, although YAML 1.2 allows it literally.  So every
conforming YAML reader accepts the tag, and parsing the output again
gives the identical tag string.  That is true for a tag that was
percent-escaped in the source document and for a tag that
cyaml_node_set_tag(3) attached, since that function accepts any
string.
 
The returned buffer is allocated with the allocator that the root
node stores.  Free it with cyaml_serialize_free(), or with
cyaml_serialize_free_mp() when the tree used a custom allocator.
 
node doesn't have to come from cyaml_parse*().  A tree that the
caller builds directly with cyaml_create_list_mp(3) and
cyaml_dictionary_set(3) has the same nesting limit that
cyaml_parse*(3) applies at parse time: A tree may nest at most 500
containers (lists and dictionaries) on any path, counting the root
when it is a container, and scalars and tags take no level.  So every
tree that a parse gives can be serialized, and the text this function
writes always parses back.  A tree that nests more containers than
that is refused and the function returns NULL for it.  The tree is
walked with an explicit worklist instead of by recursion, so the call
stack it needs is constant and doesn't grow with the nesting depth.
The limit bounds the work of one call, not the stack the caller has
to provide.
 
A NULL node serializes the same way as a CYAML_NULL node ("~\n").
That is a successful return with a non-NULL result and does not
signal a failure, and here cyaml_clone(3) differs, because it returns
NULL for a NULL node.

On success, a null-terminated YAML string on the heap.  NULL when the
function runs out of memory or node nests more than 500 containers.
This function has no error string, so the caller cannot tell the two
causes apart.

cyaml_serialize_flow(3), cyaml_serialize_stream(3), cyaml_dictionary_first(3), cyaml_serialize_free(3), cyaml_serialize_free_mp(3), cyaml_node_tag(3)