Toonade

Code Examples & Integrations

Ready-to-use code examples and integration snippets for TOON format. Copy-paste examples for your favorite languages and frameworks.

Installation

Install the Toon format SDK using npm

Install package

npm install @toon-format/toon

Basic Usage

Convert JSON to Toon and vice versa

Encode JSON to Toon

import { encode, decode } from '@toon-format/toon';

// Convert JSON to Toon
const json = {
  items: [
    { sku: "A1", qty: 2, price: 9.99 },
    { sku: "B2", qty: 1, price: 14.5 }
  ]
};

const toon = encode(json);
console.log(toon);
// Output:
// items[2]{sku,qty,price}:
//   A1,2,9.99
//   B2,1,14.5

Decode Toon to JSON

import { decode } from '@toon-format/toon';

const toonString = `items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5`;

const json = decode(toonString);
console.log(json);
// Output: { items: [{ sku: "A1", qty: 2, price: 9.99 }, ...] }

Error Handling

Handle errors gracefully

Try-catch example

import { encode, decode } from '@toon-format/toon';

try {
  const json = { name: "Toonade", version: "1.0.0" };
  const toon = encode(json);
  console.log(toon);
} catch (error) {
  console.error('Encoding failed:', error);
}

try {
  const invalidToon = "invalid toon format";
  const json = decode(invalidToon);
} catch (error) {
  console.error('Decoding failed:', error);
}

TypeScript Types

Use with TypeScript for type safety

Type definitions

import { encode, decode } from '@toon-format/toon';

interface Product {
  sku: string;
  qty: number;
  price: number;
}

interface Inventory {
  items: Product[];
}

const inventory: Inventory = {
  items: [
    { sku: "A1", qty: 2, price: 9.99 }
  ]
};

const toon = encode(inventory);
const decoded: Inventory = decode(toon) as Inventory;