Code-Beispiele & Integrationen
Einsatzbereite Code-Beispiele und Integrations-Snippets für TOON-Format. Kopieren Sie Beispiele für Ihre bevorzugten Sprachen und Frameworks.
Installation
Installieren Sie das Toon-Format SDK mit npm
Paket installieren
npm install @toon-format/toonGrundlegende Verwendung
Konvertieren Sie JSON zu Toon und umgekehrt
JSON zu Toon kodieren
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.5Toon zu JSON dekodieren
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 }, ...] }Fehlerbehandlung
Behandeln Sie Fehler elegant
Try-catch-Beispiel
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-Typen
Verwenden Sie mit TypeScript für Typsicherheit
Typdefinitionen
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;