What is toonkit?
Why TOON? Why this library?
toonkit provides two core functions:
toonToJson(input: string)— Parses TOON text into JavaScript objectsjsonToToon(obj: any)— Serializes JavaScript objects to TOON format
Why TOON?
JSON is powerful but verbose. TOON solves this by separating the schema definition from the data rows — like a typed table format. Result: smaller payloads, faster parsing, and type safety built in.
Installation
npm install toonkityarn add toonkitpnpm add toonkitImport & Functions
import { toonToJson, jsonToToon } from "toonkit";
Function Signatures
toonToJson()
Parse TOON strings into JavaScript objects
This function reads TOON-formatted text and automatically converts it to a properly-typed JavaScript object. Type codes in the schema determine how each value is parsed.
const toonString = ` device_id[1]{0:s}: DEVICE_PRO_01 battery[1]{0:n}: 87 is_active[1]{0:b}: true `; const json = toonToJson(toonString); // json = { device_id: "DEVICE_PRO_01", battery: 87, is_active: true }
Type Codes Reference
j and a must contain valid JSON. Use td for raw text that shouldn't be parsed.Complete Example
device_id[1]{0:s}: DEVICE_PRO_01 battery[1]{0:n}: 87 temperature[1]{0:n}: 36.7 is_active[1]{0:b}: true last_error[1]{0:nl}: null location[1]{0:j}: {"lat":12.9716,"lng":77.5946} tags[1]{0:a}: ["iot","health","tracker"] created_at[1]{0:td}: 03042026120000
{
device_id: "DEVICE_PRO_01",
battery: 87,
temperature: 36.7,
is_active: true,
last_error: null,
location: { lat: 12.9716, lng: 77.5946 },
tags: ["iot", "health", "tracker"],
created_at: "03042026120000"
}jsonToToon()
Convert JavaScript objects to TOON format
This function inspects your JavaScript object and automatically detects the type of each value (null, string, number, boolean, array, object). It then generates the appropriate TOON-formatted string.
Type Detection Rules
null→ nlstring→ snumber→ nboolean→ bArray→ aObject→ j (JSON)
Example
const obj = { device_id: "DEVICE_PRO_01", battery: 87, temperature: 36.7, is_active: true, last_error: null, location: { lat: 12.9716, lng: 77.5946 }, tags: ["iot", "health", "tracker"] }; const toon = jsonToToon(obj);
device_id[1]{0:s}: DEVICE_PRO_01 battery[1]{0:n}: 87 temperature[1]{0:n}: 36.7 is_active[1]{0:b}: true last_error[1]{0:nl}: null location[1]{0:j}: {"lat":12.9716,"lng":77.5946} tags[1]{0:a}: ["iot","health","tracker"]
Type Codes Reference
Complete guide to all supported data types
TOON uses single or two-letter codes to declare the type of each field. This allows toonToJson to parse values correctly and jsonToToon to detect types automatically.
Express.js Integration
Using toonkit in Node.js / Express APIs
toonkit works seamlessly with Express. Use express.text() middleware to accept TOON payloads and respond with TOON strings.
app.use(express.text()) before your routes. Without this, req.body will be empty.Testing with Postman
Manual API testing with TOON payloads
Since TOON is plain text, testing is straightforward in Postman or any HTTP client. Follow these steps:
Content-Type: text/plainExample TOON Body
device_id[1]{0:s}: DEVICE_PRO_01 battery[1]{0:n}: 87 is_active[1]{0:b}: true
Performance & Size
Why TOON is more efficient than JSON
JSON repeats every key name in every object. TOON declares the schema once, then uses compact row values. For tabular data with many rows, this saves significant bytes and parsing time.
Size Comparison
// JSON: ~145 bytes {"devices":[{"device_id":"DEVICE_PRO_01","battery":87,"is_active":true},{"device_id":"DEVICE_PRO_02","battery":92,"is_active":false}]} // TOON: ~90 bytes (38% reduction) devices[2]{device_id:s,battery:n,is_active:b}: DEVICE_PRO_01,87,true DEVICE_PRO_02,92,false
- ❌ Keys repeated per row
- ❌ Nested braces & quotes
- ❌ Heavier for tables
- ❌ More to parse
- ✅ Keys declared once
- ✅ CSV-style rows
- ✅ Compact payloads
- ✅ Faster to parse