CToon C 0.9.0
A C99 TOON/JSON serialization library
Loading...
Searching...
No Matches
CToon C API

CToon is a high-performance, zero-dependency C99 library for reading and writing the TOON serialization format, with built-in JSON interop. This reference documents the plain C API declared in ctoon.h.

Installation

# CMakeLists.txt
include(FetchContent)
FetchContent_Declare(
ctoon
GIT_REPOSITORY https://github.com/MohammadRaziei/ctoon.git
GIT_TAG v0.9.0
)
FetchContent_MakeAvailable(ctoon)
# Link the C target
target_link_libraries(my_app PRIVATE ctoon::ctoon)

Requires CMake 3.19+ and a C99-compatible compiler.

Quick example

#include "ctoon.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *src = "name: Alice\nage: 30";
ctoon_doc *doc = ctoon_read(src, strlen(src), 0);
/* Access fields */
ctoon_val *name = ctoon_obj_get(root, "name");
printf("%s\n", ctoon_get_str(name)); /* Alice */
/* Serialise back to TOON */
size_t len;
char *toon = ctoon_write(doc, &len);
free(toon);
/* Export as JSON (on by default; define CTOON_DISABLE_JSON to turn off) */
char *json = ctoon_doc_to_json(doc, 2,
CTOON_WRITE_NOFLAG, NULL, &len, NULL);
printf("%s\n", json);
free(json);
return 0;
}
static ctoon_val * ctoon_doc_get_root(ctoon_doc *doc)
Definition ctoon.h:5073
char * ctoon_doc_to_json(const ctoon_doc *doc, int indent, ctoon_write_flag flags, const ctoon_alc *alc, size_t *len, ctoon_write_err *err)
static ctoon_doc * ctoon_read(const char *dat, size_t len, ctoon_read_flag flg)
Definition ctoon.h:1077
static char * ctoon_write(const ctoon_doc *doc, size_t *len)
Definition ctoon.h:1341
static const ctoon_write_flag CTOON_WRITE_NOFLAG
Definition ctoon.h:1252
static ctoon_val * ctoon_obj_get(ctoon_val *obj, const char *key)
Definition ctoon.h:5438
static void ctoon_doc_free(ctoon_doc *doc)
Definition ctoon.h:5085
static const char * ctoon_get_str(ctoon_val *val)
Definition ctoon.h:5219

TOON spec version

The toon-format/spec release the library targets (written by CMake into ctoon.h, same time as supported_spec.conf):

CTOON_SPEC_TAG_STRING // "v4.1.2" (CTOON_SPEC_TAG_MAJOR / _MINOR / _PATCH, CTOON_SPEC_TAG_HEX)
CTOON_SPEC_DATE_STRING // "2026-07-26"
#define CTOON_SPEC_DATE_STRING
Definition ctoon.h:531
#define CTOON_SPEC_VERSION_STRING
Definition ctoon.h:530
#define CTOON_SPEC_TAG_STRING
Definition ctoon.h:538

The same values are available at runtime through ctoon_spec_version_string(), ctoon_spec_tag_hex() and ctoon_spec_date_string().

See ctoon.h for the full function reference - document I/O (ctoon_read, ctoon_write), the object/array accessors (ctoon_obj_get, ctoon_arr_get, ...), and JSON interop (ctoon_doc_to_json).