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.
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.
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.
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.
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.
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.
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.
pip install ctoon
pip install git+https://github.com/MohammadRaziei/ctoon
# 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)
ctoon::ctoon and include ctoon.h.
# 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)
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.
go get github.com/mohammadraziei/ctoon
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.
pip install ctoon
ctoon binary alongside the Python extension. After install, the ctoon command is available directly in your shell — no compiler or CMake needed.
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
cd src/bindings/matlab
ctoon_install
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.
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)
#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;
}
#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
}
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")
}
# 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
% 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');