Toonade

Ejemplos de Código e Integraciones

Ejemplos de código e integraciones listos para usar para formato TOON. Copia y pega ejemplos para tus lenguajes y frameworks favoritos.

Instalación

Instala el SDK de formato Toon usando npm

Instalar paquete

npm install @toon-format/toon

Uso Básico

Convierte JSON a Toon y viceversa

Codificar JSON a 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

Decodificar Toon a 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 }, ...] }

Manejo de Errores

Maneja errores de forma elegante

Ejemplo try-catch

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);
}

Tipos TypeScript

Usa con TypeScript para seguridad de tipos

Definiciones de tipos

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;