Exemplos de Código e Integrações
Exemplos de código e integrações prontos para usar para formato TOON. Copie e cole exemplos para suas linguagens e frameworks favoritos.
Instalação
Instale o SDK de formato Toon usando npm
Instalar pacote
npm install @toon-format/toonUso Básico
Converta JSON para Toon e vice-versa
Codificar JSON para 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.5Decodificar Toon para 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 }, ...] }Tratamento de Erros
Trate erros de forma elegante
Exemplo 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
Use com TypeScript para segurança de tipos
Definições 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;