### Install and Use TOON CLI Source: https://toonformat.dev/guide/getting-started Demonstrates how to install and use the TOON Command Line Interface (CLI). The CLI can be used without global installation via npx or installed globally using npm, pnpm, or yarn. ```bash npx @toon-format/cli input.json -o output.toon ``` ```bash npm install -g @toon-format/cli ``` ```bash pnpm add -g @toon-format/cli ``` ```bash yarn global add @toon-format/cli ``` -------------------------------- ### Install TOON TypeScript Library Source: https://toonformat.dev/guide/getting-started Installs the TOON TypeScript library using npm, pnpm, or yarn package managers. This library provides functionalities for encoding and decoding data in TOON format. ```bash npm install @toon-format/toon ``` ```bash pnpm add @toon-format/toon ``` ```bash yarn add @toon-format/toon ``` -------------------------------- ### TOON Representation of Complex Data Structure Source: https://toonformat.dev/guide/getting-started Demonstrates how TOON handles a realistic dataset containing nested objects, a primitive array, and a uniform array of structured objects. It contrasts the TOON representation with its JSON equivalent, highlighting TOON's token efficiency and structural clarity for LLMs. ```json { "context": { "task": "Our favorite hikes together", "location": "Boulder", "season": "spring_2025" }, "friends": ["ana", "luis", "sam"], "hikes": [ { "id": 1, "name": "Blue Lake Trail", "distanceKm": 7.5, "elevationGain": 320, "companion": "ana", "wasSunny": true }, { "id": 2, "name": "Ridge Overlook", "distanceKm": 9.2, "elevationGain": 540, "companion": "luis", "wasSunny": false }, { "id": 3, "name": "Wildflower Loop", "distanceKm": 5.1, "elevationGain": 180, "companion": "sam", "wasSunny": true } ] } ``` ```yaml context: task: Our favorite hikes together location: Boulder season: spring_2025 friends[3]: ana,luis,sam hikes[3]{id,name,distanceKm,elevationGain,companion,wasSunny}: 1,Blue Lake Trail,7.5,320,ana,true 2,Ridge Overlook,9.2,540,luis,false 3,Wildflower Loop,5.1,180,sam,true ``` -------------------------------- ### TOON vs JSON and YAML for Uniform Arrays Source: https://toonformat.dev/guide/getting-started Compares the token count and structure of JSON, YAML, and TOON for representing a uniform array of objects. TOON achieves compactness by declaring fields once and streaming data as rows, with explicit array length and field declarations. ```json { "users": [ { "id": 1, "name": "Alice", "role": "admin" }, { "id": 2, "name": "Bob", "role": "user" } ] } ``` ```yaml users: - id: 1 name: Alice role: admin - id: 2 name: Bob role: user ``` ```yaml users[2]{id,name,role}: 1,Alice,admin 2,Bob,user ``` -------------------------------- ### Prompting LLM to Generate TOON Output Source: https://toonformat.dev/guide/llm-prompts Illustrates how to instruct an LLM to generate data in the TOON format. By providing the expected header and specifying formatting rules, you guide the model to produce structured, efficient output. This is useful for tasks where the LLM needs to return data in a consistent, parsable format. ```markdown Data is in TOON format (2-space indent, arrays show length and fields). ```toon users[3]{id,name,role,lastLogin}: 1,Alice,admin,2025-01-15T10:30:00Z 2,Bob,user,2025-01-14T15:22:00Z 3,Charlie,user,2025-01-13T09:45:00Z ``` Task: Return only users with role "user" as TOON. Use the same header format. Set [N] to match the row count. Output only the code block. ``` -------------------------------- ### Encode Data to TOON Format (TypeScript) Source: https://toonformat.dev/guide/getting-started Encodes a JavaScript object into the TOON format using the '@toon-format/toon' TypeScript library. The example shows encoding a simple dataset containing user information. ```typescript import { encode } from '@toon-format/toon' const data = { users: [ { id: 1, name: 'Alice', role: 'admin' }, { id: 2, name: 'Bob', role: 'user' } ] } console.log(encode(data)) ``` -------------------------------- ### Decode TOON Data to JSON (TypeScript) Source: https://toonformat.dev/guide/getting-started Decodes a string in TOON format back into a JavaScript object using the '@toon-format/toon' TypeScript library. The decoded object is then stringified as JSON for display. ```typescript import { decode } from '@toon-format/toon' const toon = ` users[2]{id,name,role}: 1,Alice,admin 2,Bob,user ` const data = decode(toon) console.log(JSON.stringify(data, null, 2)) ``` -------------------------------- ### Unquoted Strings in TOON (YAML) Source: https://toonformat.dev/guide/format-overview Provides examples of strings that do not require quoting in TOON format, including those with Unicode characters, emoji, and internal spaces. ```yaml message: Hello δΈ–η•Œ πŸ‘‹ note: This has inner spaces ``` -------------------------------- ### Prompting LLM with TOON Input Data Source: https://toonformat.dev/guide/llm-prompts Demonstrates how to present TOON formatted data within a prompt to an LLM. The TOON structure, including headers and row counts, acts as self-documentation for the model. This method is effective for tasks requiring the LLM to understand and process structured data. ```markdown Data is in TOON format (2-space indent, arrays show length and fields). ```toon users[3]{id,name,role,lastLogin}: 1,Alice,admin,2025-01-15T10:30:00Z 2,Bob,user,2025-01-14T15:22:00Z 3,Charlie,user,2025-01-13T09:45:00Z ``` Task: Summarize the user roles and their last activity. ``` -------------------------------- ### TOON Nested Object Structure Source: https://toonformat.dev/guide/format-overview Illustrates how to represent nested objects in TOON using indentation. A key followed by a colon and a new line indicates the start of a nested object, with subsequent indented lines belonging to it. ```yaml user: id: 123 name: Ada ``` -------------------------------- ### Stream Large TOON Output (CLI) Source: https://toonformat.dev/guide/llm-prompts Converts JSON to TOON via streaming using the TOON CLI, which is memory-efficient for large datasets. The `--output` flag specifies the output file. ```bash toon large-dataset.json --output output.toon ``` -------------------------------- ### TOON Array of Primitive Inner Arrays Source: https://toonformat.dev/guide/format-overview Shows how TOON represents arrays containing other arrays of primitives. Each inner array gets its own header line with its length and delimiter, followed by its values. ```yaml pairs[2]: - [2]: 1,2 - [2]: 3,4 ``` -------------------------------- ### Encoding Data with Tab Delimiter for Token Efficiency Source: https://toonformat.dev/guide/llm-prompts Demonstrates using the `encode` function with a tab delimiter (`\t`) to further reduce token usage when generating TOON data. This method is beneficial for large datasets where comma-separated values might be more verbose or prone to escaping issues. ```typescript const toon = encode(data, { delimiter: '\t' }) ``` -------------------------------- ### Consume Streaming LLM Outputs (TypeScript) Source: https://toonformat.dev/guide/llm-prompts Decodes TOON incrementally from a streaming LLM response by buffering incoming chunks into lines and then decoding them. Uses the `decodeFromLines` function. ```ts import { decodeFromLines } from '@toon-format/toon' // Buffer streaming response into lines const lines: string[] = [] let buffer = '' for await (const chunk of modelStream) { buffer += chunk let index: number while ((index = buffer.indexOf('\n')) !== -1) { lines.push(buffer.slice(0, index)) buffer = buffer.slice(index + 1) } } // Decode buffered lines const data = decodeFromLines(lines) ``` -------------------------------- ### Stream Large TOON Output (TypeScript) Source: https://toonformat.dev/guide/llm-prompts Streams large TOON datasets line-by-line without building the full string in memory, preventing out-of-memory errors. Uses the `encodeLines` function from `@toon-format/toon`. ```ts import { encodeLines } from '@toon-format/toon' const largeData = await fetchThousandsOfRecords() // Stream large dataset without loading full string in memory for (const line of encodeLines(largeData, { delimiter: '\t' })) { process.stdout.write(`${line}\n`) } ``` -------------------------------- ### Validating LLM-Generated TOON with Strict Mode Source: https://toonformat.dev/guide/llm-prompts Shows how to use the `decode` function from the `@toon-format/toon` library with `strict: true` to validate TOON data generated by an LLM. Strict mode helps catch malformed TOON output, such as incorrect row counts or formatting errors, ensuring data integrity. ```typescript import { decode } from '@toon-format/toon' try { const data = decode(modelOutput, { strict: true }) // Success – data is valid } catch (error) { // Model output was malformed (count mismatch, invalid escapes, etc.) console.error('Validation failed:', error.message) } ``` -------------------------------- ### Validate TOON Response (TypeScript) Source: https://toonformat.dev/guide/llm-prompts Validates a TOON response from a model using the `decode` function with `strict: true` to catch errors. This is part of a real-world workflow for sending data to a model and verifying its output. ```ts import { decode } from '@toon-format/toon' const modelResponse = ` events[2\t]{id\tlevel\tmessage\ttimestamp}: 1\terror\tConnection timeout\t2025-01-15T10:00:00Z 4\terror\tDatabase error\t2025-01-15T10:15:00Z ` const filtered = decode(modelResponse, { strict: true }) // βœ“ Validated – model correctly filtered and adjusted [N] to 2 ``` -------------------------------- ### Key Folding Syntax (YAML) Source: https://toonformat.dev/guide/format-overview Illustrates the transformation from standard nested YAML to key-folded TOON format. Key folding collapses single-key nested objects into dotted paths. ```yaml data: metadata: items[2]: a,b ``` ```yaml data.metadata.items[2]: a,b ``` -------------------------------- ### Simple TOON Object with Primitive Values Source: https://toonformat.dev/guide/format-overview Demonstrates a basic TOON object structure where keys map to primitive values (strings, numbers, booleans). Each key-value pair is on a new line, with a single space after the colon. ```yaml id: 123 name: Ada active: true ``` -------------------------------- ### TOON Encoding and Decoding with Key Folding (TypeScript) Source: https://toonformat.dev/guide/format-overview Shows how to use the TOON library to encode data with key folding enabled and then decode it with path expansion to restore the original nested structure. This highlights the round-trip capability. ```typescript import { decode, encode } from '@toon-format/toon' const original = { data: { metadata: { items: ['a', 'b'] } } } // Encode with folding const toon = encode(original, { keyFolding: 'safe' }) // β†’ "data.metadata.items[2]: a,b" // Decode with expansion const restored = decode(toon, { expandPaths: 'safe' }) // β†’ { data: { metadata: { items: ['a', 'b'] } } } ``` -------------------------------- ### TOON Delimiter Options (YAML) Source: https://toonformat.dev/guide/format-overview Demonstrates the syntax for defining different delimiters (comma, tab, pipe) within TOON arrays. The delimiter is specified in the array header. ```yaml items[2]{sku,name,qty,price}: A1,Widget,2,9.99 B2,Gadget,1,14.5 ``` ```yaml items[2\t]{sku\tname\qty\tprice}: A1\tWidget\t2\t9.99 B2\tGadget\t1\t14.5 ``` ```yaml items[2|]{sku|name|qty|price}: A1|Widget|2|9.99 B2|Gadget|1|14.5 ``` -------------------------------- ### TOON Tabular Array of Objects Source: https://toonformat.dev/guide/format-overview Demonstrates the tabular format for arrays where objects share the same primitive keys. The header specifies the array length and field names, with subsequent lines containing the values for each row. ```yaml items[2]{sku,qty,price}: A1,2,9.99 B2,1,14.5 ``` ```yaml users[2]{id,name,role}: 1,Alice Admin,admin 2,"Bob Smith",user ``` -------------------------------- ### TOON Empty Array Representation Source: https://toonformat.dev/guide/format-overview Illustrates the TOON syntax for representing an empty array. This is achieved by specifying the array length as zero (`[0]`) with no elements following. ```yaml items[0]: ``` -------------------------------- ### TOON Array with Object List Items Source: https://toonformat.dev/guide/format-overview Shows how objects are represented as list items within a TOON array. Each object is indented under the hyphen marker, maintaining its own key-value structure. ```yaml items[2]: - id: 1 name: First - id: 2 name: Second extra: true ``` -------------------------------- ### TOON Array Header with Custom Delimiter Source: https://toonformat.dev/guide/format-overview Demonstrates the syntax for TOON array headers, including specifying a custom delimiter. The delimiter character is placed within the length brackets. ```yaml key[N]<{fields}>: ``` -------------------------------- ### TOON Mixed Array with List Format Source: https://toonformat.dev/guide/format-overview Illustrates TOON's list format for arrays that do not conform to tabular requirements. Elements are denoted by a hyphen marker (`- `) at a deeper indentation level than the array header. ```yaml items[3]: - 1 - a: 1 - text ``` -------------------------------- ### Custom Serialization with toJSON in TypeScript Source: https://toonformat.dev/guide/format-overview Demonstrates how to use the toJSON method for custom serialization of objects in ToonFormat. The toJSON method is called to serialize the object, and its result is then normalized. This method takes precedence over default normalization rules for types like Date, Array, Set, and Map. ```typescript const obj = { data: 'example', toJSON() { return { info: this.data } } } encode(obj) // info: example ``` -------------------------------- ### TOON Primitive Array (Inline) Source: https://toonformat.dev/guide/format-overview Shows an inline representation of an array containing primitive values. The array length is specified in brackets, and values are separated by a delimiter (comma by default). ```yaml tags[3]: admin,ops,dev ``` -------------------------------- ### TOON Array with Tabular Field in List Item Object Source: https://toonformat.dev/guide/format-overview Demonstrates a TOON array where a list item object contains a tabular array as its first field. The tabular header is on the hyphen line, followed by indented rows and other fields. ```yaml items[1]: - users[2]{id,name}: 1,Ada 2,Bob status: active ``` ```yaml items[1]: - users[2]{id,name}: 1,Ada 2,Bob ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.