CToon Documentation
v0.4.0 GitHub
C · C++ · Python · Go · MATLAB

CToon Documentation

A high-performance serialisation library for the TOON format — a compact, human-readable alternative to JSON. Built on a fast C core, exposing idiomatic APIs for C, C++, Python, and Go.

C99
Core language
5
Language bindings
MIT
License
3.19+
CMake required
Language Documentation
Full API reference for each language binding, generated from source.
C API C99

Core library interface. Covers document parsing with ctoon_read, serialisation with ctoon_write, JSON export via ctoon_doc_to_json, and full value-traversal API including ctoon_obj_get, ctoon_arr_get, and typed getters. Zero dependencies beyond the C standard library.

Generated with Doxygen
Open C Reference
C++ API C++11

Header-only RAII wrappers around the C core. Covers ctoon::document, ctoon::value, ctoon::make_document, EncodeOptions, DecodeOptions, and read/write flag enums. All objects manage lifetime automatically — no manual free calls needed.

Generated with Doxygen
Open C++ Reference
Python API Python 3.9+

nanobind-based extension module. Documents loads, dumps, load, dump, JSON interop functions (loads_json, dumps_json), and the ReadFlag / WriteFlag / Delimiter enums. Ships pre-built wheels for Linux, macOS, and Windows.

Generated with Sphinx
Open Python Reference
Go API Go 1.21+

CGo bindings that wrap the C core. Covers Loads, Dumps, LoadsJSON, DumpsJSON, file I/O helpers (EncodeToFile, DecodeFromFile), and encode/decode option structs. Requires CGo-enabled build environment.

Generated with godoc
Open Go Reference
CLI Reference v0.4.0

Command-line tool for converting between JSON and TOON formats. Covers all options and flags including --delimiter, --length-marker, --stats, stdin piping, and forced encode/decode modes. Auto-detects conversion direction from file extension.

Written in Markdown
Open CLI Reference
MATLAB API R2014b+

MEX-based binding for the CToon C library. Documents ctoon_encode, ctoon_decode, ctoon_read, and ctoon_write. Works with any MATLAB R2014b or newer — no toolboxes required. Build with ctoon_build or via CMake.

Generated from .m docstrings
Open MATLAB Reference
Installation
Add CToon to your project in seconds.
bash
pip install ctoon
Requires Python 3.9+. No runtime dependencies — wheels ship a pre-built native extension for Linux, macOS, and Windows.
Install the latest development build directly from source: pip install git+https://github.com/MohammadRaziei/ctoon
CMake — FetchContent
# 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. Link against ctoon::ctoon and include ctoon.h.
CMake — FetchContent
# 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::ctoonpp)
Requires CMake 3.19+ and a C++11-compatible compiler. Link against ctoon::ctoonpp and include ctoon.hpp. The C++ wrapper is header-only on top of the C core — all RAII lifetime management is included automatically.
bash
go get github.com/mohammadraziei/ctoon
Requires Go 1.21+ with CGo enabled (CGO_ENABLED=1). The module bundles the C core source — no separate C library install is needed. A C compiler (gcc / clang) must be available on the build host.
bash
pip install ctoon
The Python package ships a pre-built ctoon binary alongside the Python extension. After install, the ctoon command is available directly in your shell — no compiler or CMake needed.
bash
git clone https://github.com/MohammadRaziei/ctoon.git
cd ctoon
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)

# Install to system (adds `ctoon` to PATH)
sudo cmake --install build
Requires CMake 3.19+ and a C99 compiler. Builds the binary from source — useful if you want a custom build configuration or are on a platform without a pre-built wheel.
matlab
cd src/bindings/matlab
ctoon_install
Requires MATLAB R2014b+ and a C compiler (mex -setup C). ctoon_install compiles the MEX gateway and permanently adds the binding to your MATLAB path. For MATLAB R2022b+ you can also use buildtool mex.
Quick Examples
Encode, decode, and interop with JSON in a few lines of code.
python
import ctoon

# Encode Python dict → TOON string
toon = ctoon.dumps({"name": "Alice", "age": 30})
# name: Alice
# age: 30

# Decode TOON string → Python dict
data = ctoon.loads(toon)

# File I/O — path strings or file-like objects
ctoon.dump(data, "out.toon")
data = ctoon.load("out.toon")

# JSON interop
json_str = ctoon.dumps_json(data, indent=2)
data     = ctoon.loads_json(json_str)
ctoon.dump_json(data, "out.json")

# Advanced encode flags
from ctoon import WriteFlag, Delimiter
toon = ctoon.dumps(data,
    indent=4,
    delimiter=Delimiter.PIPE,
    flags=WriteFlag.LENGTH_MARKER | WriteFlag.NEWLINE_AT_END)
c
#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);
    ctoon_val *root = ctoon_doc_get_root(doc);

    /* 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);

    ctoon_doc_free(doc);
    return 0;
}
c++
#include "ctoon.hpp"
#include <iostream>

int main() {
    // Parse a TOON document
    auto doc  = ctoon::document::parse("name: Alice\nage: 30");
    auto root = doc.root();
    std::cout << root["name"].get_str().str() << "\n";  // Alice
    std::cout << root["age"].get_uint()        << "\n";  // 30

    // Serialise to TOON and JSON
    std::cout << doc.write().c_str()    << "\n";
    std::cout << doc.to_json(2).c_str() << "\n";

    // Build a document programmatically
    auto mdoc = ctoon::make_document();
    auto obj  = mdoc.make_obj();
    mdoc.set_root(obj);
    obj.obj_put(mdoc.make_str("city"), mdoc.make_str("Tehran"));
    obj.obj_put(mdoc.make_str("pop"),  mdoc.make_uint(9000000));
    std::cout << mdoc.to_json(0).c_str() << "\n";

    // Parse from JSON
    auto jdoc = ctoon::document::from_json(R"({"x":1,"y":2})");
    std::cout << jdoc.root()["x"].get_uint() << "\n";  // 1
}
go
package main

import (
    "fmt"
    ctoon "github.com/mohammadraziei/ctoon"
)

func main() {
    data := map[string]interface{}{
        "name": "Alice",
        "age":  int64(30),
    }

    // Encode → TOON
    toon, _ := ctoon.Dumps(data)
    fmt.Println(toon)

    // Decode ← TOON
    val, _ := ctoon.Loads(toon)
    fmt.Println(val)

    // File I/O
    ctoon.EncodeToFile(data, "out.toon", ctoon.DefaultEncodeOptions())
    val, _ = ctoon.DecodeFromFile("out.toon", ctoon.DefaultDecodeOptions())

    // JSON interop
    json, _ := ctoon.DumpsJSON(data, 2)
    fmt.Println(json)
    ctoon.DumpJSON(data, "out.json")
}
bash
# JSON → TOON (auto-detected from extension)
ctoon input.json
ctoon input.json -o output.toon

# TOON → JSON with pretty-printing
ctoon input.toon
ctoon input.toon -o output.json -i 4

# Read from stdin
cat data.json | ctoon -e -

# Encoder options
ctoon input.json --delimiter pipe     # comma (default), tab, pipe
ctoon input.json --length-marker      # emit items[#3]: …
ctoon input.json --stats              # print byte-savings summary

# Force conversion direction
ctoon -e input.json   # force encode  JSON → TOON
ctoon -d input.toon   # force decode  TOON → JSON
matlab
% Encode a MATLAB struct → TOON string
s = ctoon_encode(struct('name', 'Alice', 'age', uint64(30), 'active', true));
% name: Alice
% age: 30
% active: true

% Decode TOON string → MATLAB struct
v = ctoon_decode(s);
v.name    % 'Alice'
v.age     % uint64(30)
v.active  % true

% File I/O
ctoon_write(v, 'data.toon');
v = ctoon_read('data.toon');