Skip to main content
The EncodeOptions interface allows you to customize how JavaScript values are encoded to TOON format. All options are optional and have sensible defaults.

Interface

Options

number
default:2
Number of spaces per indentation level. Controls how deeply nested structures are visually indented in the output.
Delimiter
default:"DELIMITERS.comma"
Delimiter to use for separating values in tabular array rows and inline primitive arrays.Available delimiters:
  • DELIMITERS.comma - , (default)
  • DELIMITERS.tab - \t
  • DELIMITERS.pipe - |
'off' | 'safe'
default:"'off'"
Enable key folding to collapse single-key wrapper chains into dotted paths.When set to 'safe', nested objects with single keys are collapsed into dotted notation (e.g., data.metadata.items instead of nested indentation). This reduces token usage for deeply nested structures.
number
default:"Infinity"
Maximum number of segments to fold when keyFolding is enabled.Controls how deep the folding can go in single-key chains. Values 0 or 1 have no practical effect (treated as effectively disabled).
EncodeReplacer
default:"undefined"
A function to transform or filter values during encoding.Called for the root value and every nested property/element. Similar to JSON.stringify’s replacer, but with path tracking. Return undefined to omit properties/elements (root cannot be omitted).Type signature:
Parameters:
  • key - The property key or array index (as string). Empty string ('') for root value.
  • value - The normalized JsonValue at this location.
  • path - Array representing the path from root to this value.
Returns:
  • The replacement value (will be normalized again), or undefined to omit.
  • For root value, returning undefined means “no change” (don’t omit root).

Type: ResolvedEncodeOptions

The internal type representing fully resolved encoding options with all defaults applied:
This type is used internally by the encoder after resolving all option defaults. You typically don’t need to use this type directly.

Usage Examples

Basic encoding with custom options

Streaming with options

Advanced replacer usage

See Also