CToon C 0.6.1
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 main
)
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 (CTOON_ENABLE_JSON=1, default) */
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:4889
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:1023
static char * ctoon_write(const ctoon_doc *doc, size_t *len)
Definition ctoon.h:1287
static const ctoon_write_flag CTOON_WRITE_NOFLAG
Definition ctoon.h:1198
static ctoon_val * ctoon_obj_get(ctoon_val *obj, const char *key)
Definition ctoon.h:5254
static void ctoon_doc_free(ctoon_doc *doc)
Definition ctoon.h:4901
static const char * ctoon_get_str(ctoon_val *val)
Definition ctoon.h:5035

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).