/** * JSON/JSONC configuration file tools. * Read, validate, deep merge, and diff JSON config files with comment support. */ /** Strip single-line (//) and multi-line comments from JSONC content. */ function stripJsoncComments(text: string): string { let result = ""; let i = 0; let inString = false; let escaped = false; while (i < text.length) { const ch = text[i]; const next = text[i + 1]; if (inString) { result += ch; if (escaped) { escaped = false; } else if (ch === "\\") { escaped = true; } else if (ch === '"') { inString = false; } i++; continue; } if (ch === '"') { inString = true; result += ch; i++; continue; } if (ch === "/" && next === "/") { // Skip until end of line i += 2; while (i < text.length && text[i] !== "\n") i++; continue; } if (ch === "/" && next === "*") { // Skip until closing */ i += 2; while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++; i += 2; // skip */ continue; } result += ch; i++; } return result; } /** Parse JSONC (JSON with comments) content. */ export function parseJsonc(text: string): unknown { const stripped = stripJsoncComments(text); // Also handle trailing commas by removing them const cleaned = stripped.replace(/,\s*([\]}])/g, "$1"); return JSON.parse(cleaned); } /** Check if a value is a plain object (not array, not null). */ function isPlainObject(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } /** Deep merge two objects. Values in `overlay` override values in `base`. */ export function deepMerge(base: unknown, overlay: unknown): unknown { if (!isPlainObject(base) || !isPlainObject(overlay)) { return overlay; } const result: Record = { ...base }; for (const key of Object.keys(overlay)) { if (key in result && isPlainObject(result[key]) && isPlainObject(overlay[key])) { result[key] = deepMerge(result[key], overlay[key]); } else { result[key] = overlay[key]; } } return result; } export interface DiffEntry { path: string; type: "added" | "removed" | "changed"; oldValue?: unknown; newValue?: unknown; } /** Find differences between two parsed config objects. */ export function findDifferences( a: unknown, b: unknown, prefix: string = "" ): DiffEntry[] { const diffs: DiffEntry[] = []; if (!isPlainObject(a) || !isPlainObject(b)) { if (JSON.stringify(a) !== JSON.stringify(b)) { diffs.push({ path: prefix || "(root)", type: "changed", oldValue: a, newValue: b }); } return diffs; } const allKeys = new Set([...Object.keys(a), ...Object.keys(b)]); for (const key of allKeys) { const fullPath = prefix ? `${prefix}.${key}` : key; const inA = key in a; const inB = key in b; if (inA && !inB) { diffs.push({ path: fullPath, type: "removed", oldValue: a[key] }); } else if (!inA && inB) { diffs.push({ path: fullPath, type: "added", newValue: b[key] }); } else if (isPlainObject(a[key]) && isPlainObject(b[key])) { diffs.push(...findDifferences(a[key], b[key], fullPath)); } else if (JSON.stringify(a[key]) !== JSON.stringify(b[key])) { diffs.push({ path: fullPath, type: "changed", oldValue: a[key], newValue: b[key] }); } } return diffs; } /** Validate that content is valid JSON/JSONC and return parsed result or error. */ export function validateJson(text: string): { valid: boolean; data?: unknown; error?: string } { try { const data = parseJsonc(text); return { valid: true, data }; } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); return { valid: false, error: message }; } }