export function toJson(obj: any, spacing: boolean = false): string { return JSON.stringify(obj, jsonReplacer, spacing ? 2 : undefined); } export function jsonReplacer(key: string, value: any): any { if (typeof value === 'bigint') { return value.toString(); } if (value instanceof Date) { return value.toISOString(); } return value; } export function safeToJson(v: any): string { try { return typeof v === 'string' ? v : toJson(v); } catch { return '[unserializable]'; } }