Versions 172 to 318
Lines 1–27 at 318 (were 1–7 at 172)
1+/** Internal values may expose an exact streaming representation without changing public toJSON. */ 2+const streamingValues = new WeakMap<object, () => unknown>(); 3+export function registerJsonChunkValue(value: object, representation: () => unknown): void { 4+ streamingValues.set(value, representation); 7+/** A JSON string backed by bounded, replayable UTF-16 chunks. */ 8+export class JsonStringChunks { 9+ readonly chunks: () => Iterable<string>; 10+ constructor(chunks: () => Iterable<string>) { 11+ this.chunks = chunks; 14+ return [...this.chunks()].join(""); 118/** JSON.stringify-compatible chunks for canonical data, without a whole-value string. 219 * Individual input strings and object key lists still belong to the caller. 4−export function* jsonValueChunks(input: unknown): IterableIterator<string> {
21+export function* jsonValueChunks( 23+ options: { maxDepth?: number; unboxPrimitives?: boolean } = {}, 24+): IterableIterator<string> { 525 const active = new Set<object>(); 626 function* string(value: string): IterableIterator<string> { Lines 36–74 at 318 (were 16–35 at 172)
1838 const normalize = (value: unknown, key: string): unknown => { 39+ if (value instanceof JsonStringChunks) return value; 1940 if (value !== null && typeof value === "object") { 41+ const streaming = streamingValues.get(value); 42+ if (streaming) return streaming(); 2043 const toJSON = (value as { toJSON?: unknown }).toJSON; 21− if (typeof toJSON === "function") return toJSON.call(value, key);
44+ if (typeof toJSON === "function") value = toJSON.call(value, key); 46+ if (options.unboxPrimitives && (value instanceof String || value instanceof Number || value instanceof Boolean)) 47+ return value.valueOf(); 2550 const omitted = (value: unknown) => value === undefined || typeof value === "function" || typeof value === "symbol"; 2651 function* encode(value: unknown): IterableIterator<string> { 27− if (typeof value === "string") yield* string(value);
52+ if (value instanceof JsonStringChunks) { 55+ for (const chunk of value.chunks()) { 56+ if (typeof chunk !== "string" || chunk.length > 16384) throw Error("JSON string chunk exceeds bound"); 57+ const text = pending + chunk; 58+ let end = text.length; 59+ const last = text.charCodeAt(end - 1); 60+ if (last >= 0xd800 && last <= 0xdbff) end--; 61+ pending = text.slice(end); 62+ if (end) yield JSON.stringify(text.slice(0, end)).slice(1, -1); 64+ if (pending) yield JSON.stringify(pending).slice(1, -1); 66+ } else if (typeof value === "string") yield* string(value); 2867 else if (value === null || typeof value === "boolean" || typeof value === "number") yield JSON.stringify(value); 2968 else if (typeof value !== "object" || value === null) throw Error("unsupported canonical JSON value"); 3170 if (active.has(value)) throw Error("cyclic canonical JSON value"); 32− if (active.size >= 128) throw Error("canonical JSON depth exceeds bound");
71+ if (active.size >= (options.maxDepth ?? 128)) throw Error("canonical JSON depth exceeds bound"); 3574 if (Array.isArray(value)) {