import { z } from "zod"; import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js"; const jsonValueTypeSchema = z.enum([ "object", "array", "string", "number", "boolean", "null", ]); type JsonValueType = z.infer; const jsonShapePathSegmentSchema = z.discriminatedUnion("kind", [ z.object({ kind: z.literal("property"), name: z.string() }), z.object({ kind: z.literal("array-element") }), ]); type JsonShapePathSegment = z.infer; /** Value-free structural summary of one approved JSON payload. */ export const jsonShapeSchema = z.object({ root_type: jsonValueTypeSchema, node_count: z.number().int().min(1), max_depth_observed: z.number().int().min(0), properties: z.array( z.object({ path: z.array(jsonShapePathSegmentSchema).min(1), types: z.array(jsonValueTypeSchema).min(1), observations: z.number().int().min(1), }), ), }); export type JsonShape = z.infer; const arrayElementKey = Symbol("array-element"); interface ShapeNode { readonly path: JsonShapePathSegment[]; readonly types: Set; observations: number; children?: Map; } /** Parse approved JSON and immediately discard values after iterative shape inference. */ export const inferJsonShape = (text: string): JsonShape | null => { let root: unknown; try { root = JSON.parse(text); } catch (cause: unknown) { // Invalid JSON is represented by the null return. void cause; return null; } const properties: ShapeNode[] = []; const rootShape: ShapeNode = { path: [], types: new Set(), observations: 0, }; // Each structural path owns one accumulator, shared by all matching array rows. const childShape = ( parent: ShapeNode, key: string | typeof arrayElementKey, ): ShapeNode => { const children = (parent.children ??= new Map()); const existing = children.get(key); if (existing !== undefined) return existing; const node: ShapeNode = { path: [ ...parent.path, typeof key === "string" ? { kind: "property", name: key } : { kind: "array-element" }, ], types: new Set(), observations: 0, }; children.set(key, node); properties.push(node); return node; }; const pending: Array<{ readonly value: unknown; readonly shape: ShapeNode; }> = [{ value: root, shape: rootShape }]; let nodeCount = 0; let maxDepthObserved = 0; while (pending.length > 0) { const current = pending.pop(); if (current === undefined) break; nodeCount += 1; maxDepthObserved = Math.max(maxDepthObserved, current.shape.path.length); current.shape.types.add(jsonValueType(current.value)); current.shape.observations += 1; if (Array.isArray(current.value)) { if (current.value.length === 0) continue; const shape = childShape(current.shape, arrayElementKey); for (let index = current.value.length - 1; index >= 0; index -= 1) pending.push({ value: current.value[index], shape }); continue; } if (!isRecord(current.value)) continue; const allEntries = Object.entries(current.value); for (let index = allEntries.length - 1; index >= 0; index -= 1) { const entry = allEntries[index]; if (entry === undefined) continue; const [name, value] = entry; pending.push({ value, shape: childShape(current.shape, name) }); } } return jsonShapeSchema.parse({ root_type: jsonValueType(root), node_count: nodeCount, max_depth_observed: maxDepthObserved, properties: properties .map((value) => ({ path: value.path, types: [...value.types].sort(), observations: value.observations, })) .sort((left, right) => compareShapePaths(left.path, right.path)), }); }; const jsonValueType = (value: unknown): JsonValueType => { if (value === null) return "null"; if (Array.isArray(value)) return "array"; switch (typeof value) { case "string": return "string"; case "number": return "number"; case "boolean": return "boolean"; default: return "object"; } }; const isRecord = (value: unknown): value is Readonly> => typeof value === "object" && value !== null && !Array.isArray(value); const compareShapePaths = ( left: readonly JsonShapePathSegment[], right: readonly JsonShapePathSegment[], ): number => { for (let index = 0; index < Math.min(left.length, right.length); index += 1) { const a = left[index]; const b = right[index]; if (a === undefined || b === undefined) break; const order = compareUnicodeCodePoints(a.kind, b.kind) || (a.kind === "property" && b.kind === "property" ? compareUnicodeCodePoints(a.name, b.name) : 0); if (order !== 0) return order; } return left.length - right.length; };