zon-format
Version:
ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors
124 lines (123 loc) • 3.49 kB
TypeScript
import { type ZonDocumentMetadata } from './versioning';
export interface DecodeOptions {
strict?: boolean;
/** Extract version metadata from decoded data (default: false) */
extractMetadata?: boolean;
}
export interface DecodeResult {
data: any;
metadata?: ZonDocumentMetadata;
}
export declare class ZonDecoder {
private strict;
private currentLine;
constructor(options?: DecodeOptions);
/**
* Decodes ZON format string to original data structure.
*
* @param zonStr - ZON formatted string
* @param options - Optional decode options
* @returns Decoded data or DecodeResult if extractMetadata is true
*/
decode(zonStr: string, options?: DecodeOptions): any | DecodeResult;
/**
* Parses table header line.
*
* @param line - Header line to parse
* @returns Tuple of [tableName, tableInfo]
*/
private _parseTableHeader;
/**
* Parses a table row with sparse encoding support.
*
* @param line - Row line to parse
* @param table - Table information
* @returns Parsed row object
*/
private _parseTableRow;
/**
* Checks if string is a URL.
*
* @param s - String to check
* @returns True if URL format
*/
private _isURL;
/**
* Checks if string is a timestamp.
*
* @param s - String to check
* @returns True if timestamp format
*/
private _isTimestamp;
/**
* Reconstructs table from parsed rows.
*
* @param table - Table information
* @returns Array of reconstructed objects
*/
private _reconstructTable;
/**
* Recursively parses ZON nested structures.
*
* @param text - Text to parse
* @param depth - Current nesting depth
* @returns Parsed value
*/
private _parseZonNode;
/**
* Finds first occurrence of delimiter outside quotes.
*
* @param text - Text to search
* @param delim - Delimiter to find
* @returns Index of delimiter or -1
*/
private _findDelimiter;
/**
* Splits text by delimiter while respecting quotes and nesting.
*
* @param text - Text to split
* @param delim - Delimiter character (default: ',')
* @param splitByNewline - Whether to treat newline as delimiter (default: false)
* @returns Array of split parts
*/
private _splitByDelimiter;
/**
* Parses a primitive value.
*
* @param val - Value string to parse
* @returns Parsed primitive value
*/
/**
* Parses a value, handling primitives and nested ZON structures.
*
* @param val - Value string to parse
* @returns Parsed value
*/
private _parseValue;
/**
* Unflattens dictionary with dotted keys.
*
* @param d - Flattened dictionary
* @returns Unflattened object
*/
private _unflatten;
/**
* Checks if quotes are balanced in string.
*/
private _areQuotesBalanced;
/**
* Splits object properties respecting indentation.
*
* @param text - Object content
* @returns Array of property strings
*/
private _splitObjectProperties;
}
/**
* Decodes ZON format string to original data v1.1.0.
*
* @param data - ZON format string
* @param options - Decode options
* @returns Decoded data or DecodeResult if extractMetadata is true
*/
export declare function decode(data: string, options?: DecodeOptions): any | DecodeResult;