@typia/utils 12.0.0-dev.20260309 → 12.0.0-dev.20260311

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/lib/converters/LlmSchemaConverter.d.ts +0 -1
  2. package/lib/converters/LlmSchemaConverter.js +4 -31
  3. package/lib/converters/LlmSchemaConverter.js.map +1 -1
  4. package/lib/converters/LlmSchemaConverter.mjs +2 -32
  5. package/lib/converters/LlmSchemaConverter.mjs.map +1 -1
  6. package/lib/http/HttpLlm.js +4 -5
  7. package/lib/http/HttpLlm.js.map +1 -1
  8. package/lib/http/HttpLlm.mjs +0 -1
  9. package/lib/http/HttpLlm.mjs.map +1 -1
  10. package/lib/http/internal/HttpLlmApplicationComposer.js +3 -4
  11. package/lib/http/internal/HttpLlmApplicationComposer.js.map +1 -1
  12. package/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -2
  13. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  14. package/lib/index.mjs +9 -9
  15. package/lib/utils/LlmJson.mjs +9 -2
  16. package/lib/utils/LlmJson.mjs.map +1 -1
  17. package/lib/utils/internal/stringifyValidationFailure.js +17 -15
  18. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
  19. package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
  20. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
  21. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  22. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  23. package/package.json +2 -2
  24. package/src/converters/LlmSchemaConverter.ts +617 -647
  25. package/src/converters/OpenApiConverter.ts +285 -285
  26. package/src/converters/index.ts +5 -5
  27. package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
  28. package/src/converters/internal/LlmParametersComposer.ts +52 -52
  29. package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
  30. package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
  31. package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
  32. package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
  33. package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
  34. package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
  35. package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
  36. package/src/http/HttpError.ts +107 -107
  37. package/src/http/HttpLlm.ts +166 -167
  38. package/src/http/HttpMigration.ts +92 -92
  39. package/src/http/index.ts +3 -3
  40. package/src/http/internal/HttpLlmApplicationComposer.ts +360 -361
  41. package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
  42. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  43. package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
  44. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  45. package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
  46. package/src/index.ts +4 -4
  47. package/src/utils/ArrayUtil.ts +42 -42
  48. package/src/utils/LlmJson.ts +141 -141
  49. package/src/utils/MapUtil.ts +15 -15
  50. package/src/utils/NamingConvention.ts +205 -205
  51. package/src/utils/Singleton.ts +17 -17
  52. package/src/utils/StringUtil.ts +14 -14
  53. package/src/utils/dedent.ts +57 -57
  54. package/src/utils/index.ts +8 -8
  55. package/src/utils/internal/EndpointUtil.ts +44 -44
  56. package/src/utils/internal/JsonDescriptor.ts +70 -70
  57. package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
  58. package/src/utils/internal/coerceLlmArguments.ts +314 -314
  59. package/src/utils/internal/parseLenientJson.ts +894 -894
  60. package/src/utils/internal/stringifyValidationFailure.ts +415 -411
  61. package/src/validators/LlmTypeChecker.ts +402 -402
  62. package/src/validators/OpenApiTypeChecker.ts +297 -297
  63. package/src/validators/OpenApiV3TypeChecker.ts +70 -70
  64. package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
  65. package/src/validators/OpenApiValidator.ts +94 -94
  66. package/src/validators/SwaggerV2TypeChecker.ts +71 -71
  67. package/src/validators/functional/_isBigintString.ts +8 -8
  68. package/src/validators/functional/_isFormatByte.ts +7 -7
  69. package/src/validators/functional/_isFormatDate.ts +3 -3
  70. package/src/validators/functional/_isFormatDateTime.ts +4 -4
  71. package/src/validators/functional/_isFormatDuration.ts +4 -4
  72. package/src/validators/functional/_isFormatEmail.ts +4 -4
  73. package/src/validators/functional/_isFormatHostname.ts +4 -4
  74. package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
  75. package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
  76. package/src/validators/functional/_isFormatIpv4.ts +4 -4
  77. package/src/validators/functional/_isFormatIpv6.ts +4 -4
  78. package/src/validators/functional/_isFormatIri.ts +3 -3
  79. package/src/validators/functional/_isFormatIriReference.ts +4 -4
  80. package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
  81. package/src/validators/functional/_isFormatPassword.ts +1 -1
  82. package/src/validators/functional/_isFormatRegex.ts +8 -8
  83. package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
  84. package/src/validators/functional/_isFormatTime.ts +4 -4
  85. package/src/validators/functional/_isFormatUri.ts +6 -6
  86. package/src/validators/functional/_isFormatUriReference.ts +5 -5
  87. package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
  88. package/src/validators/functional/_isFormatUrl.ts +4 -4
  89. package/src/validators/functional/_isFormatUuid.ts +3 -3
  90. package/src/validators/functional/_isUniqueItems.ts +159 -159
  91. package/src/validators/index.ts +14 -14
  92. package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
  93. package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
  94. package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
  95. package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
  96. package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
  97. package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
  98. package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
  99. package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
  100. package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
  101. package/src/validators/internal/OpenApiStationValidator.ts +115 -115
  102. package/src/validators/internal/OpenApiStringValidator.ts +88 -88
  103. package/src/validators/internal/OpenApiTupleValidator.ts +55 -55
@@ -1,894 +1,894 @@
1
- import { DeepPartial, IJsonParseResult } from "@typia/interface";
2
-
3
- /**
4
- * Parse lenient JSON that may be incomplete or malformed.
5
- *
6
- * Handles:
7
- *
8
- * - Unclosed brackets `{`, `[` - parses as much as possible
9
- * - Trailing commas `[1, 2, ]` - ignores them
10
- * - Unclosed strings `"hello` - returns partial string
11
- * - Junk text before JSON (LLM often adds explanatory text)
12
- * - Markdown code blocks (extracts content from `json ... `)
13
- * - Incomplete keywords like `tru`, `fal`, `nul`
14
- * - Unicode escape sequences including surrogate pairs (emoji)
15
- * - JavaScript-style comments (single-line and multi-line)
16
- * - Unquoted object keys (JavaScript identifier style)
17
- *
18
- * @param input Raw JSON string (potentially incomplete)
19
- * @returns Parse result with data, original input, and any errors
20
- * @internal
21
- */
22
- export function parseLenientJson<T>(input: string): IJsonParseResult<T> {
23
- // Try native JSON.parse first (faster for valid JSON)
24
- try {
25
- return {
26
- success: true,
27
- data: JSON.parse(input) as T,
28
- };
29
- } catch {
30
- // Fall back to lenient parser
31
- }
32
-
33
- // Extract markdown code block if present
34
- const codeBlockContent: string | null = extractMarkdownCodeBlock(input);
35
- const jsonSource: string =
36
- codeBlockContent !== null ? codeBlockContent : input;
37
-
38
- // Check if input is empty or whitespace-only
39
- const trimmed: string = jsonSource.trim();
40
- if (trimmed.length === 0) {
41
- return {
42
- success: false,
43
- data: undefined as DeepPartial<T>,
44
- input,
45
- errors: [
46
- {
47
- path: "$input",
48
- expected: "JSON value",
49
- value: "empty input",
50
- },
51
- ],
52
- };
53
- }
54
-
55
- // Check if input starts with a primitive value (no junk prefix skipping needed)
56
- if (startsWithPrimitive(trimmed)) {
57
- const errors: IJsonParseResult.IError[] = [];
58
- const parser: LenientJsonParser = new LenientJsonParser(jsonSource, errors);
59
- const data: unknown = parser.parse();
60
- if (errors.length > 0) {
61
- return { success: false, data: data as DeepPartial<T>, input, errors };
62
- }
63
- return { success: true, data: data as T };
64
- }
65
-
66
- // Find JSON start position (skip junk prefix from LLM)
67
- const jsonStart: number = findJsonStart(jsonSource);
68
- if (jsonStart === -1) {
69
- // No object/array found - check if there's a primitive after skipping comments
70
- const skipped: string = skipCommentsAndWhitespace(jsonSource);
71
- if (skipped.length > 0 && startsWithPrimitive(skipped)) {
72
- const errors: IJsonParseResult.IError[] = [];
73
- const parser: LenientJsonParser = new LenientJsonParser(
74
- jsonSource,
75
- errors,
76
- );
77
- const data: unknown = parser.parse();
78
- if (errors.length > 0) {
79
- return { success: false, data: data as DeepPartial<T>, input, errors };
80
- }
81
- return { success: true, data: data as T };
82
- }
83
- // No valid JSON found - return failure
84
- return {
85
- success: false,
86
- data: undefined as DeepPartial<T>,
87
- input,
88
- errors: [
89
- {
90
- path: "$input",
91
- expected: "JSON value",
92
- value: jsonSource,
93
- },
94
- ],
95
- };
96
- }
97
-
98
- // Extract JSON portion (skip junk prefix)
99
- const jsonInput: string =
100
- jsonStart > 0 ? jsonSource.slice(jsonStart) : jsonSource;
101
-
102
- const errors: IJsonParseResult.IError[] = [];
103
- const parser: LenientJsonParser = new LenientJsonParser(jsonInput, errors);
104
- const data: unknown = parser.parse();
105
-
106
- if (errors.length > 0) {
107
- return {
108
- success: false,
109
- data: data as DeepPartial<T>,
110
- input,
111
- errors,
112
- };
113
- }
114
- return {
115
- success: true,
116
- data: data as T,
117
- };
118
- }
119
-
120
- /**
121
- * Maximum nesting depth to prevent stack overflow attacks.
122
- *
123
- * @internal
124
- */
125
- const MAX_DEPTH: number = 512;
126
-
127
- /**
128
- * Check if a string is a valid 4-character hexadecimal string.
129
- *
130
- * @internal
131
- */
132
- function isHexString(s: string): boolean {
133
- if (s.length !== 4) return false;
134
- for (let i = 0; i < 4; i++) {
135
- const c: number = s.charCodeAt(i);
136
- if (
137
- !((c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102))
138
- ) {
139
- return false;
140
- }
141
- }
142
- return true;
143
- }
144
-
145
- /**
146
- * Extract JSON content from markdown code block if present.
147
- *
148
- * LLM outputs often wrap JSON in markdown code blocks like:
149
- *
150
- * Here is your result:
151
- *
152
- * ```json
153
- * { "name": "test" }
154
- * ```
155
- *
156
- * This function extracts the content between the backticks.
157
- *
158
- * IMPORTANT: Only extracts if the input doesn't already start with JSON. If
159
- * input (after trim) starts with `{`, `[`, or `"`, it's already JSON and any
160
- * markdown inside is part of a string value.
161
- *
162
- * @param input Text that may contain markdown code block
163
- * @returns Extracted content or null if no code block found
164
- * @internal
165
- */
166
- function extractMarkdownCodeBlock(input: string): string | null {
167
- // Must be ```json specifically, not just ```
168
- const codeBlockStart: number = input.indexOf("```json");
169
- if (codeBlockStart === -1) return null;
170
-
171
- // Check if input already starts with JSON (after trimming whitespace)
172
- // If so, don't extract - the markdown is inside a JSON string value
173
- const trimmed: string = input.trimStart();
174
- if (trimmed.length > 0) {
175
- const firstChar: string = trimmed[0]!;
176
- if (firstChar === "{" || firstChar === "[" || firstChar === '"') {
177
- return null;
178
- }
179
- }
180
-
181
- // Find the end of the opening line (after ```json)
182
- let contentStart: number = codeBlockStart + 7; // length of "```json"
183
- while (contentStart < input.length && input[contentStart] !== "\n") {
184
- contentStart++;
185
- }
186
- if (contentStart >= input.length) return null;
187
- contentStart++; // skip the newline
188
-
189
- // Find the closing ```
190
- const codeBlockEnd: number = input.indexOf("```", contentStart);
191
- if (codeBlockEnd === -1) {
192
- // No closing ``` - return everything after opening
193
- return input.slice(contentStart);
194
- }
195
-
196
- return input.slice(contentStart, codeBlockEnd);
197
- }
198
-
199
- /**
200
- * Find the start position of JSON object/array content in text that may have
201
- * junk prefix.
202
- *
203
- * LLM outputs often contain text before JSON like:
204
- *
205
- * - "Here is your JSON: {"name": "test"}"
206
- * - "Sure! [1, 2, 3]"
207
- *
208
- * This function skips over comments and strings to find the real JSON start.
209
- * Primitive values (strings, numbers, booleans) are handled directly by the
210
- * parser.
211
- *
212
- * @param input Text that may contain JSON with junk prefix
213
- * @returns Index of first `{` or `[` outside comments/strings, or -1 if not
214
- * found
215
- * @internal
216
- */
217
- function findJsonStart(input: string): number {
218
- let pos: number = 0;
219
- const len: number = input.length;
220
-
221
- while (pos < len) {
222
- const ch: string = input[pos]!;
223
-
224
- // Found JSON start
225
- if (ch === "{" || ch === "[") {
226
- return pos;
227
- }
228
-
229
- // Skip single-line comment
230
- if (ch === "/" && pos + 1 < len && input[pos + 1] === "/") {
231
- pos += 2;
232
- while (pos < len && input[pos] !== "\n" && input[pos] !== "\r") {
233
- pos++;
234
- }
235
- continue;
236
- }
237
-
238
- // Skip multi-line comment
239
- if (ch === "/" && pos + 1 < len && input[pos + 1] === "*") {
240
- pos += 2;
241
- while (pos + 1 < len) {
242
- if (input[pos] === "*" && input[pos + 1] === "/") {
243
- pos += 2;
244
- break;
245
- }
246
- pos++;
247
- }
248
- // If unclosed comment, move to end
249
- if (pos + 1 >= len) {
250
- pos = len;
251
- }
252
- continue;
253
- }
254
-
255
- // Skip string literal (to avoid matching { or [ inside strings)
256
- if (ch === '"') {
257
- pos++;
258
- while (pos < len) {
259
- if (input[pos] === "\\") {
260
- pos += 2; // skip escape sequence
261
- continue;
262
- }
263
- if (input[pos] === '"') {
264
- pos++;
265
- break;
266
- }
267
- pos++;
268
- }
269
- continue;
270
- }
271
-
272
- pos++;
273
- }
274
-
275
- return -1;
276
- }
277
-
278
- /**
279
- * Skip leading comments and whitespace from input.
280
- *
281
- * @param input Text that may start with comments or whitespace
282
- * @returns Input with leading comments and whitespace removed
283
- * @internal
284
- */
285
- function skipCommentsAndWhitespace(input: string): string {
286
- let pos: number = 0;
287
- const len: number = input.length;
288
-
289
- while (pos < len) {
290
- const ch: string = input[pos]!;
291
-
292
- // Skip whitespace
293
- if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
294
- pos++;
295
- continue;
296
- }
297
-
298
- // Skip single-line comment
299
- if (ch === "/" && pos + 1 < len && input[pos + 1] === "/") {
300
- pos += 2;
301
- while (pos < len && input[pos] !== "\n" && input[pos] !== "\r") {
302
- pos++;
303
- }
304
- continue;
305
- }
306
-
307
- // Skip multi-line comment
308
- if (ch === "/" && pos + 1 < len && input[pos + 1] === "*") {
309
- pos += 2;
310
- while (pos + 1 < len) {
311
- if (input[pos] === "*" && input[pos + 1] === "/") {
312
- pos += 2;
313
- break;
314
- }
315
- pos++;
316
- }
317
- if (pos + 1 >= len) {
318
- pos = len;
319
- }
320
- continue;
321
- }
322
-
323
- // Not whitespace or comment
324
- break;
325
- }
326
-
327
- return input.slice(pos);
328
- }
329
-
330
- /**
331
- * Check if input starts with a valid JSON primitive token.
332
- *
333
- * @param input Trimmed input string
334
- * @returns True if input starts with a primitive value
335
- * @internal
336
- */
337
- function startsWithPrimitive(input: string): boolean {
338
- if (input.length === 0) return false;
339
- const ch: string = input[0]!;
340
- // String
341
- if (ch === '"') return true;
342
- // Number (digit or minus)
343
- if ((ch >= "0" && ch <= "9") || ch === "-") return true;
344
- // Keywords
345
- if (
346
- input.startsWith("true") ||
347
- input.startsWith("false") ||
348
- input.startsWith("null")
349
- )
350
- return true;
351
- // Partial keywords (note: "null" requires at least 2 chars to match parseKeywordOrIdentifier logic)
352
- if (
353
- "true".startsWith(input) ||
354
- "false".startsWith(input) ||
355
- ("null".startsWith(input) && input.length >= 2)
356
- )
357
- return true;
358
- // Boolean string variants (note: "n" is intentionally excluded)
359
- const lower: string = input.toLowerCase();
360
- if (
361
- lower === "yes" ||
362
- lower === "y" ||
363
- lower === "on" ||
364
- lower === "no" ||
365
- lower === "off"
366
- )
367
- return true;
368
- return false;
369
- }
370
-
371
- /**
372
- * Lenient JSON parser that handles incomplete JSON.
373
- *
374
- * @internal
375
- */
376
- class LenientJsonParser {
377
- private pos: number = 0;
378
- private depth: number = 0;
379
- private readonly input: string;
380
- private readonly errors: IJsonParseResult.IError[];
381
-
382
- constructor(input: string, errors: IJsonParseResult.IError[]) {
383
- this.input = input;
384
- this.errors = errors;
385
- }
386
-
387
- parse(): unknown {
388
- this.skipWhitespace();
389
- if (this.pos >= this.input.length) {
390
- return undefined;
391
- }
392
- return this.parseValue("$input");
393
- }
394
-
395
- private parseValue(path: string): unknown {
396
- this.skipWhitespace();
397
-
398
- if (this.pos >= this.input.length) {
399
- return undefined;
400
- }
401
-
402
- // Check for maximum depth to prevent stack overflow
403
- if (this.depth >= MAX_DEPTH) {
404
- this.errors.push({
405
- path,
406
- expected: "value (max depth exceeded)",
407
- value: undefined,
408
- });
409
- return undefined;
410
- }
411
-
412
- const char: string = this.input[this.pos]!;
413
-
414
- if (char === "{") return this.parseObject(path);
415
- if (char === "[") return this.parseArray(path);
416
- if (char === '"') return this.parseString();
417
- if (char === "-" || (char >= "0" && char <= "9")) return this.parseNumber();
418
-
419
- // Handle keywords (true, false, null) or invalid identifiers
420
- if (this.isIdentifierStart(char)) {
421
- return this.parseKeywordOrIdentifier(path);
422
- }
423
-
424
- // Don't skip structural characters - let the caller handle them
425
- const ch: string = this.input[this.pos]!;
426
- if (ch === "}" || ch === "]" || ch === ",") {
427
- // Not an error - just no value here (e.g., {"a":} or [,])
428
- return undefined;
429
- }
430
-
431
- this.errors.push({
432
- path,
433
- expected: "JSON value",
434
- value: this.getErrorContext(),
435
- });
436
- // Skip the problematic character and try to continue
437
- this.pos++;
438
- return undefined;
439
- }
440
-
441
- private getErrorContext(): string {
442
- // Get surrounding context for better error messages
443
- const start: number = Math.max(0, this.pos - 10);
444
- const end: number = Math.min(this.input.length, this.pos + 20);
445
- const before: string = this.input.slice(start, this.pos);
446
- const after: string = this.input.slice(this.pos, end);
447
- return (
448
- (start > 0 ? "..." : "") +
449
- before +
450
- "→" +
451
- after +
452
- (end < this.input.length ? "..." : "")
453
- );
454
- }
455
-
456
- private parseKeywordOrIdentifier(path: string): unknown {
457
- // Extract the token (sequence of identifier characters)
458
- const start: number = this.pos;
459
- while (
460
- this.pos < this.input.length &&
461
- this.isIdentifierChar(this.input[this.pos]!)
462
- ) {
463
- this.pos++;
464
- }
465
- const token: string = this.input.slice(start, this.pos);
466
-
467
- // Check for complete or partial keyword matches
468
- if (token === "true") return true;
469
- if (token === "false") return false;
470
- if (token === "null") return null;
471
-
472
- // Boolean string coercion: "yes", "y", "on" -> true, "no", "off" -> false
473
- // Note: "n" is intentionally NOT handled (neither null nor false)
474
- const lower: string = token.toLowerCase();
475
- if (lower === "yes" || lower === "y" || lower === "on") return true;
476
- if (lower === "no" || lower === "off") return false;
477
-
478
- // Partial match for lenient parsing (e.g., "tru" -> true, "fal" -> false)
479
- if ("true".startsWith(token) && token.length > 0) return true;
480
- if ("false".startsWith(token) && token.length > 0) return false;
481
- if ("null".startsWith(token) && token.length >= 2) return null;
482
-
483
- // Check if this looks like a string with missing opening quote (e.g., abcdefg")
484
- if (this.pos < this.input.length && this.input[this.pos] === '"') {
485
- // Treat as unquoted string value - skip the errant closing quote and return as string
486
- this.pos++; // skip the closing quote
487
- this.errors.push({
488
- path,
489
- expected: "quoted string",
490
- value: "missing opening quote for '" + token + "'",
491
- });
492
- return token;
493
- }
494
-
495
- // Invalid identifier as value - provide helpful error message
496
- this.errors.push({
497
- path,
498
- expected: "JSON value (string, number, boolean, null, object, or array)",
499
- value: "unquoted string '" + token + "' - did you forget quotes?",
500
- });
501
- // Skip to next comma, closing brace/bracket for recovery
502
- this.skipToRecoveryPoint();
503
- return undefined;
504
- }
505
-
506
- private skipToRecoveryPoint(): void {
507
- while (this.pos < this.input.length) {
508
- const ch: string = this.input[this.pos]!;
509
- if (ch === "," || ch === "}" || ch === "]") {
510
- return;
511
- }
512
- this.pos++;
513
- }
514
- }
515
-
516
- private parseObject(path: string): Record<string, unknown> {
517
- const result: Record<string, unknown> = {};
518
- this.pos++; // skip '{'
519
- this.depth++;
520
- this.skipWhitespace();
521
-
522
- while (this.pos < this.input.length) {
523
- this.skipWhitespace();
524
-
525
- // Handle end of object or end of input
526
- if (this.pos >= this.input.length || this.input[this.pos] === "}") {
527
- if (this.pos < this.input.length) this.pos++; // skip '}'
528
- this.depth--;
529
- return result;
530
- }
531
-
532
- // Skip trailing comma
533
- if (this.input[this.pos] === ",") {
534
- this.pos++;
535
- this.skipWhitespace();
536
- continue;
537
- }
538
-
539
- // Parse key (quoted string or unquoted identifier)
540
- let key: string;
541
- if (this.input[this.pos] === '"') {
542
- key = this.parseString();
543
- } else if (this.isIdentifierStart(this.input[this.pos]!)) {
544
- key = this.parseIdentifier();
545
- } else {
546
- this.errors.push({
547
- path,
548
- expected: "string key",
549
- value: this.input[this.pos],
550
- });
551
- // Try to recover by skipping to next meaningful character
552
- this.depth--;
553
- return result;
554
- }
555
- if (typeof key !== "string") {
556
- this.depth--;
557
- return result;
558
- }
559
-
560
- this.skipWhitespace();
561
-
562
- // Expect colon - but if we're at end of input, it's just incomplete (not an error)
563
- if (this.pos >= this.input.length) {
564
- this.depth--;
565
- return result;
566
- }
567
- if (this.input[this.pos] !== ":") {
568
- this.errors.push({
569
- path: path + "." + key,
570
- expected: "':'",
571
- value: this.input[this.pos],
572
- });
573
- this.depth--;
574
- return result;
575
- }
576
- this.pos++; // skip ':'
577
-
578
- this.skipWhitespace();
579
-
580
- // Parse value
581
- if (this.pos >= this.input.length) {
582
- // No value - incomplete but not an error for lenient parsing
583
- this.depth--;
584
- return result;
585
- }
586
-
587
- const value: unknown = this.parseValue(path + "." + key);
588
- result[key] = value;
589
-
590
- this.skipWhitespace();
591
-
592
- // Handle comma or end
593
- if (this.pos < this.input.length && this.input[this.pos] === ",") {
594
- this.pos++;
595
- }
596
- }
597
-
598
- this.depth--;
599
- return result;
600
- }
601
-
602
- private parseArray(path: string): unknown[] {
603
- const result: unknown[] = [];
604
- this.pos++; // skip '['
605
- this.depth++;
606
- this.skipWhitespace();
607
-
608
- let index: number = 0;
609
- while (this.pos < this.input.length) {
610
- this.skipWhitespace();
611
-
612
- // Handle end of array or end of input
613
- if (this.pos >= this.input.length || this.input[this.pos] === "]") {
614
- if (this.pos < this.input.length) this.pos++; // skip ']'
615
- this.depth--;
616
- return result;
617
- }
618
-
619
- // Skip trailing comma
620
- if (this.input[this.pos] === ",") {
621
- this.pos++;
622
- this.skipWhitespace();
623
- continue;
624
- }
625
-
626
- // Parse value
627
- const prevPos: number = this.pos;
628
- const value: unknown = this.parseValue(path + "[" + index + "]");
629
-
630
- // Guard: if parseValue didn't advance, skip unexpected char to prevent infinite loop
631
- if (this.pos === prevPos && this.pos < this.input.length) {
632
- this.pos++;
633
- continue;
634
- }
635
-
636
- result.push(value);
637
- index++;
638
-
639
- this.skipWhitespace();
640
-
641
- // Handle comma or end
642
- if (this.pos < this.input.length && this.input[this.pos] === ",") {
643
- this.pos++;
644
- }
645
- }
646
-
647
- this.depth--;
648
- return result;
649
- }
650
-
651
- private parseString(): string {
652
- this.pos++; // skip opening '"'
653
- let result: string = "";
654
- let escaped: boolean = false;
655
-
656
- while (this.pos < this.input.length) {
657
- const char: string = this.input[this.pos]!;
658
-
659
- if (escaped) {
660
- switch (char) {
661
- case '"':
662
- result += '"';
663
- break;
664
- case "\\":
665
- result += "\\";
666
- break;
667
- case "/":
668
- result += "/";
669
- break;
670
- case "b":
671
- result += "\b";
672
- break;
673
- case "f":
674
- result += "\f";
675
- break;
676
- case "n":
677
- result += "\n";
678
- break;
679
- case "r":
680
- result += "\r";
681
- break;
682
- case "t":
683
- result += "\t";
684
- break;
685
- case "u":
686
- // Parse unicode escape
687
- if (this.pos + 4 <= this.input.length) {
688
- const hex: string = this.input.slice(this.pos + 1, this.pos + 5);
689
- if (isHexString(hex)) {
690
- const highCode: number = parseInt(hex, 16);
691
- this.pos += 4;
692
-
693
- // Check for surrogate pair (emoji and characters > U+FFFF)
694
- if (
695
- highCode >= 0xd800 &&
696
- highCode <= 0xdbff &&
697
- this.pos + 6 <= this.input.length &&
698
- this.input[this.pos + 1] === "\\" &&
699
- this.input[this.pos + 2] === "u"
700
- ) {
701
- const lowHex: string = this.input.slice(
702
- this.pos + 3,
703
- this.pos + 7,
704
- );
705
- if (isHexString(lowHex)) {
706
- const lowCode: number = parseInt(lowHex, 16);
707
- if (lowCode >= 0xdc00 && lowCode <= 0xdfff) {
708
- result += String.fromCharCode(highCode, lowCode);
709
- this.pos += 6;
710
- break;
711
- }
712
- }
713
- }
714
- result += String.fromCharCode(highCode);
715
- } else {
716
- // Invalid hex - preserve escape sequence literally
717
- result += "\\u" + hex;
718
- this.pos += 4;
719
- }
720
- } else {
721
- // Incomplete unicode escape - add partial sequence
722
- const partial: string = this.input.slice(this.pos + 1);
723
- result += "\\u" + partial;
724
- this.pos = this.input.length - 1;
725
- }
726
- break;
727
- default:
728
- result += char;
729
- }
730
- escaped = false;
731
- this.pos++;
732
- continue;
733
- }
734
-
735
- if (char === "\\") {
736
- escaped = true;
737
- this.pos++;
738
- continue;
739
- }
740
-
741
- if (char === '"') {
742
- this.pos++; // skip closing '"'
743
- return result;
744
- }
745
-
746
- result += char;
747
- this.pos++;
748
- }
749
-
750
- // Unclosed string - return what we have (lenient)
751
- return result;
752
- }
753
-
754
- private parseNumber(): number {
755
- const start: number = this.pos;
756
-
757
- // Handle negative sign
758
- if (this.input[this.pos] === "-") {
759
- this.pos++;
760
- }
761
-
762
- // Parse integer part
763
- while (
764
- this.pos < this.input.length &&
765
- this.input[this.pos]! >= "0" &&
766
- this.input[this.pos]! <= "9"
767
- ) {
768
- this.pos++;
769
- }
770
-
771
- // Parse decimal part
772
- if (this.pos < this.input.length && this.input[this.pos] === ".") {
773
- this.pos++;
774
- while (
775
- this.pos < this.input.length &&
776
- this.input[this.pos]! >= "0" &&
777
- this.input[this.pos]! <= "9"
778
- ) {
779
- this.pos++;
780
- }
781
- }
782
-
783
- // Parse exponent
784
- if (
785
- this.pos < this.input.length &&
786
- (this.input[this.pos] === "e" || this.input[this.pos] === "E")
787
- ) {
788
- this.pos++;
789
- if (
790
- this.pos < this.input.length &&
791
- (this.input[this.pos] === "+" || this.input[this.pos] === "-")
792
- ) {
793
- this.pos++;
794
- }
795
- while (
796
- this.pos < this.input.length &&
797
- this.input[this.pos]! >= "0" &&
798
- this.input[this.pos]! <= "9"
799
- ) {
800
- this.pos++;
801
- }
802
- }
803
-
804
- const numStr: string = this.input.slice(start, this.pos);
805
- const num: number = Number(numStr);
806
- return Number.isNaN(num) ? 0 : num;
807
- }
808
-
809
- private isIdentifierStart(ch: string): boolean {
810
- return (
811
- (ch >= "a" && ch <= "z") ||
812
- (ch >= "A" && ch <= "Z") ||
813
- ch === "_" ||
814
- ch === "$"
815
- );
816
- }
817
-
818
- private isIdentifierChar(ch: string): boolean {
819
- return (
820
- (ch >= "a" && ch <= "z") ||
821
- (ch >= "A" && ch <= "Z") ||
822
- (ch >= "0" && ch <= "9") ||
823
- ch === "_" ||
824
- ch === "$"
825
- );
826
- }
827
-
828
- private parseIdentifier(): string {
829
- const start: number = this.pos;
830
- while (
831
- this.pos < this.input.length &&
832
- this.isIdentifierChar(this.input[this.pos]!)
833
- ) {
834
- this.pos++;
835
- }
836
- return this.input.slice(start, this.pos);
837
- }
838
-
839
- private skipWhitespace(): void {
840
- while (this.pos < this.input.length) {
841
- const ch: string = this.input[this.pos]!;
842
-
843
- // Skip standard whitespace
844
- if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
845
- this.pos++;
846
- continue;
847
- }
848
-
849
- // Skip single-line comment: // ...
850
- if (
851
- ch === "/" &&
852
- this.pos + 1 < this.input.length &&
853
- this.input[this.pos + 1] === "/"
854
- ) {
855
- this.pos += 2;
856
- while (
857
- this.pos < this.input.length &&
858
- this.input[this.pos] !== "\n" &&
859
- this.input[this.pos] !== "\r"
860
- ) {
861
- this.pos++;
862
- }
863
- continue;
864
- }
865
-
866
- // Skip multi-line comment: /* ... */
867
- if (
868
- ch === "/" &&
869
- this.pos + 1 < this.input.length &&
870
- this.input[this.pos + 1] === "*"
871
- ) {
872
- this.pos += 2;
873
- while (this.pos + 1 < this.input.length) {
874
- if (
875
- this.input[this.pos] === "*" &&
876
- this.input[this.pos + 1] === "/"
877
- ) {
878
- this.pos += 2;
879
- break;
880
- }
881
- this.pos++;
882
- }
883
- // Handle unclosed comment - move to end
884
- if (this.pos + 1 >= this.input.length) {
885
- this.pos = this.input.length;
886
- }
887
- continue;
888
- }
889
-
890
- // Not whitespace or comment
891
- break;
892
- }
893
- }
894
- }
1
+ import { DeepPartial, IJsonParseResult } from "@typia/interface";
2
+
3
+ /**
4
+ * Parse lenient JSON that may be incomplete or malformed.
5
+ *
6
+ * Handles:
7
+ *
8
+ * - Unclosed brackets `{`, `[` - parses as much as possible
9
+ * - Trailing commas `[1, 2, ]` - ignores them
10
+ * - Unclosed strings `"hello` - returns partial string
11
+ * - Junk text before JSON (LLM often adds explanatory text)
12
+ * - Markdown code blocks (extracts content from `json ... `)
13
+ * - Incomplete keywords like `tru`, `fal`, `nul`
14
+ * - Unicode escape sequences including surrogate pairs (emoji)
15
+ * - JavaScript-style comments (single-line and multi-line)
16
+ * - Unquoted object keys (JavaScript identifier style)
17
+ *
18
+ * @param input Raw JSON string (potentially incomplete)
19
+ * @returns Parse result with data, original input, and any errors
20
+ * @internal
21
+ */
22
+ export function parseLenientJson<T>(input: string): IJsonParseResult<T> {
23
+ // Try native JSON.parse first (faster for valid JSON)
24
+ try {
25
+ return {
26
+ success: true,
27
+ data: JSON.parse(input) as T,
28
+ };
29
+ } catch {
30
+ // Fall back to lenient parser
31
+ }
32
+
33
+ // Extract markdown code block if present
34
+ const codeBlockContent: string | null = extractMarkdownCodeBlock(input);
35
+ const jsonSource: string =
36
+ codeBlockContent !== null ? codeBlockContent : input;
37
+
38
+ // Check if input is empty or whitespace-only
39
+ const trimmed: string = jsonSource.trim();
40
+ if (trimmed.length === 0) {
41
+ return {
42
+ success: false,
43
+ data: undefined as DeepPartial<T>,
44
+ input,
45
+ errors: [
46
+ {
47
+ path: "$input",
48
+ expected: "JSON value",
49
+ value: "empty input",
50
+ },
51
+ ],
52
+ };
53
+ }
54
+
55
+ // Check if input starts with a primitive value (no junk prefix skipping needed)
56
+ if (startsWithPrimitive(trimmed)) {
57
+ const errors: IJsonParseResult.IError[] = [];
58
+ const parser: LenientJsonParser = new LenientJsonParser(jsonSource, errors);
59
+ const data: unknown = parser.parse();
60
+ if (errors.length > 0) {
61
+ return { success: false, data: data as DeepPartial<T>, input, errors };
62
+ }
63
+ return { success: true, data: data as T };
64
+ }
65
+
66
+ // Find JSON start position (skip junk prefix from LLM)
67
+ const jsonStart: number = findJsonStart(jsonSource);
68
+ if (jsonStart === -1) {
69
+ // No object/array found - check if there's a primitive after skipping comments
70
+ const skipped: string = skipCommentsAndWhitespace(jsonSource);
71
+ if (skipped.length > 0 && startsWithPrimitive(skipped)) {
72
+ const errors: IJsonParseResult.IError[] = [];
73
+ const parser: LenientJsonParser = new LenientJsonParser(
74
+ jsonSource,
75
+ errors,
76
+ );
77
+ const data: unknown = parser.parse();
78
+ if (errors.length > 0) {
79
+ return { success: false, data: data as DeepPartial<T>, input, errors };
80
+ }
81
+ return { success: true, data: data as T };
82
+ }
83
+ // No valid JSON found - return failure
84
+ return {
85
+ success: false,
86
+ data: undefined as DeepPartial<T>,
87
+ input,
88
+ errors: [
89
+ {
90
+ path: "$input",
91
+ expected: "JSON value",
92
+ value: jsonSource,
93
+ },
94
+ ],
95
+ };
96
+ }
97
+
98
+ // Extract JSON portion (skip junk prefix)
99
+ const jsonInput: string =
100
+ jsonStart > 0 ? jsonSource.slice(jsonStart) : jsonSource;
101
+
102
+ const errors: IJsonParseResult.IError[] = [];
103
+ const parser: LenientJsonParser = new LenientJsonParser(jsonInput, errors);
104
+ const data: unknown = parser.parse();
105
+
106
+ if (errors.length > 0) {
107
+ return {
108
+ success: false,
109
+ data: data as DeepPartial<T>,
110
+ input,
111
+ errors,
112
+ };
113
+ }
114
+ return {
115
+ success: true,
116
+ data: data as T,
117
+ };
118
+ }
119
+
120
+ /**
121
+ * Maximum nesting depth to prevent stack overflow attacks.
122
+ *
123
+ * @internal
124
+ */
125
+ const MAX_DEPTH: number = 512;
126
+
127
+ /**
128
+ * Check if a string is a valid 4-character hexadecimal string.
129
+ *
130
+ * @internal
131
+ */
132
+ function isHexString(s: string): boolean {
133
+ if (s.length !== 4) return false;
134
+ for (let i = 0; i < 4; i++) {
135
+ const c: number = s.charCodeAt(i);
136
+ if (
137
+ !((c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102))
138
+ ) {
139
+ return false;
140
+ }
141
+ }
142
+ return true;
143
+ }
144
+
145
+ /**
146
+ * Extract JSON content from markdown code block if present.
147
+ *
148
+ * LLM outputs often wrap JSON in markdown code blocks like:
149
+ *
150
+ * Here is your result:
151
+ *
152
+ * ```json
153
+ * { "name": "test" }
154
+ * ```
155
+ *
156
+ * This function extracts the content between the backticks.
157
+ *
158
+ * IMPORTANT: Only extracts if the input doesn't already start with JSON. If
159
+ * input (after trim) starts with `{`, `[`, or `"`, it's already JSON and any
160
+ * markdown inside is part of a string value.
161
+ *
162
+ * @param input Text that may contain markdown code block
163
+ * @returns Extracted content or null if no code block found
164
+ * @internal
165
+ */
166
+ function extractMarkdownCodeBlock(input: string): string | null {
167
+ // Must be ```json specifically, not just ```
168
+ const codeBlockStart: number = input.indexOf("```json");
169
+ if (codeBlockStart === -1) return null;
170
+
171
+ // Check if input already starts with JSON (after trimming whitespace)
172
+ // If so, don't extract - the markdown is inside a JSON string value
173
+ const trimmed: string = input.trimStart();
174
+ if (trimmed.length > 0) {
175
+ const firstChar: string = trimmed[0]!;
176
+ if (firstChar === "{" || firstChar === "[" || firstChar === '"') {
177
+ return null;
178
+ }
179
+ }
180
+
181
+ // Find the end of the opening line (after ```json)
182
+ let contentStart: number = codeBlockStart + 7; // length of "```json"
183
+ while (contentStart < input.length && input[contentStart] !== "\n") {
184
+ contentStart++;
185
+ }
186
+ if (contentStart >= input.length) return null;
187
+ contentStart++; // skip the newline
188
+
189
+ // Find the closing ```
190
+ const codeBlockEnd: number = input.indexOf("```", contentStart);
191
+ if (codeBlockEnd === -1) {
192
+ // No closing ``` - return everything after opening
193
+ return input.slice(contentStart);
194
+ }
195
+
196
+ return input.slice(contentStart, codeBlockEnd);
197
+ }
198
+
199
+ /**
200
+ * Find the start position of JSON object/array content in text that may have
201
+ * junk prefix.
202
+ *
203
+ * LLM outputs often contain text before JSON like:
204
+ *
205
+ * - "Here is your JSON: {"name": "test"}"
206
+ * - "Sure! [1, 2, 3]"
207
+ *
208
+ * This function skips over comments and strings to find the real JSON start.
209
+ * Primitive values (strings, numbers, booleans) are handled directly by the
210
+ * parser.
211
+ *
212
+ * @param input Text that may contain JSON with junk prefix
213
+ * @returns Index of first `{` or `[` outside comments/strings, or -1 if not
214
+ * found
215
+ * @internal
216
+ */
217
+ function findJsonStart(input: string): number {
218
+ let pos: number = 0;
219
+ const len: number = input.length;
220
+
221
+ while (pos < len) {
222
+ const ch: string = input[pos]!;
223
+
224
+ // Found JSON start
225
+ if (ch === "{" || ch === "[") {
226
+ return pos;
227
+ }
228
+
229
+ // Skip single-line comment
230
+ if (ch === "/" && pos + 1 < len && input[pos + 1] === "/") {
231
+ pos += 2;
232
+ while (pos < len && input[pos] !== "\n" && input[pos] !== "\r") {
233
+ pos++;
234
+ }
235
+ continue;
236
+ }
237
+
238
+ // Skip multi-line comment
239
+ if (ch === "/" && pos + 1 < len && input[pos + 1] === "*") {
240
+ pos += 2;
241
+ while (pos + 1 < len) {
242
+ if (input[pos] === "*" && input[pos + 1] === "/") {
243
+ pos += 2;
244
+ break;
245
+ }
246
+ pos++;
247
+ }
248
+ // If unclosed comment, move to end
249
+ if (pos + 1 >= len) {
250
+ pos = len;
251
+ }
252
+ continue;
253
+ }
254
+
255
+ // Skip string literal (to avoid matching { or [ inside strings)
256
+ if (ch === '"') {
257
+ pos++;
258
+ while (pos < len) {
259
+ if (input[pos] === "\\") {
260
+ pos += 2; // skip escape sequence
261
+ continue;
262
+ }
263
+ if (input[pos] === '"') {
264
+ pos++;
265
+ break;
266
+ }
267
+ pos++;
268
+ }
269
+ continue;
270
+ }
271
+
272
+ pos++;
273
+ }
274
+
275
+ return -1;
276
+ }
277
+
278
+ /**
279
+ * Skip leading comments and whitespace from input.
280
+ *
281
+ * @param input Text that may start with comments or whitespace
282
+ * @returns Input with leading comments and whitespace removed
283
+ * @internal
284
+ */
285
+ function skipCommentsAndWhitespace(input: string): string {
286
+ let pos: number = 0;
287
+ const len: number = input.length;
288
+
289
+ while (pos < len) {
290
+ const ch: string = input[pos]!;
291
+
292
+ // Skip whitespace
293
+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
294
+ pos++;
295
+ continue;
296
+ }
297
+
298
+ // Skip single-line comment
299
+ if (ch === "/" && pos + 1 < len && input[pos + 1] === "/") {
300
+ pos += 2;
301
+ while (pos < len && input[pos] !== "\n" && input[pos] !== "\r") {
302
+ pos++;
303
+ }
304
+ continue;
305
+ }
306
+
307
+ // Skip multi-line comment
308
+ if (ch === "/" && pos + 1 < len && input[pos + 1] === "*") {
309
+ pos += 2;
310
+ while (pos + 1 < len) {
311
+ if (input[pos] === "*" && input[pos + 1] === "/") {
312
+ pos += 2;
313
+ break;
314
+ }
315
+ pos++;
316
+ }
317
+ if (pos + 1 >= len) {
318
+ pos = len;
319
+ }
320
+ continue;
321
+ }
322
+
323
+ // Not whitespace or comment
324
+ break;
325
+ }
326
+
327
+ return input.slice(pos);
328
+ }
329
+
330
+ /**
331
+ * Check if input starts with a valid JSON primitive token.
332
+ *
333
+ * @param input Trimmed input string
334
+ * @returns True if input starts with a primitive value
335
+ * @internal
336
+ */
337
+ function startsWithPrimitive(input: string): boolean {
338
+ if (input.length === 0) return false;
339
+ const ch: string = input[0]!;
340
+ // String
341
+ if (ch === '"') return true;
342
+ // Number (digit or minus)
343
+ if ((ch >= "0" && ch <= "9") || ch === "-") return true;
344
+ // Keywords
345
+ if (
346
+ input.startsWith("true") ||
347
+ input.startsWith("false") ||
348
+ input.startsWith("null")
349
+ )
350
+ return true;
351
+ // Partial keywords (note: "null" requires at least 2 chars to match parseKeywordOrIdentifier logic)
352
+ if (
353
+ "true".startsWith(input) ||
354
+ "false".startsWith(input) ||
355
+ ("null".startsWith(input) && input.length >= 2)
356
+ )
357
+ return true;
358
+ // Boolean string variants (note: "n" is intentionally excluded)
359
+ const lower: string = input.toLowerCase();
360
+ if (
361
+ lower === "yes" ||
362
+ lower === "y" ||
363
+ lower === "on" ||
364
+ lower === "no" ||
365
+ lower === "off"
366
+ )
367
+ return true;
368
+ return false;
369
+ }
370
+
371
+ /**
372
+ * Lenient JSON parser that handles incomplete JSON.
373
+ *
374
+ * @internal
375
+ */
376
+ class LenientJsonParser {
377
+ private pos: number = 0;
378
+ private depth: number = 0;
379
+ private readonly input: string;
380
+ private readonly errors: IJsonParseResult.IError[];
381
+
382
+ constructor(input: string, errors: IJsonParseResult.IError[]) {
383
+ this.input = input;
384
+ this.errors = errors;
385
+ }
386
+
387
+ parse(): unknown {
388
+ this.skipWhitespace();
389
+ if (this.pos >= this.input.length) {
390
+ return undefined;
391
+ }
392
+ return this.parseValue("$input");
393
+ }
394
+
395
+ private parseValue(path: string): unknown {
396
+ this.skipWhitespace();
397
+
398
+ if (this.pos >= this.input.length) {
399
+ return undefined;
400
+ }
401
+
402
+ // Check for maximum depth to prevent stack overflow
403
+ if (this.depth >= MAX_DEPTH) {
404
+ this.errors.push({
405
+ path,
406
+ expected: "value (max depth exceeded)",
407
+ value: undefined,
408
+ });
409
+ return undefined;
410
+ }
411
+
412
+ const char: string = this.input[this.pos]!;
413
+
414
+ if (char === "{") return this.parseObject(path);
415
+ if (char === "[") return this.parseArray(path);
416
+ if (char === '"') return this.parseString();
417
+ if (char === "-" || (char >= "0" && char <= "9")) return this.parseNumber();
418
+
419
+ // Handle keywords (true, false, null) or invalid identifiers
420
+ if (this.isIdentifierStart(char)) {
421
+ return this.parseKeywordOrIdentifier(path);
422
+ }
423
+
424
+ // Don't skip structural characters - let the caller handle them
425
+ const ch: string = this.input[this.pos]!;
426
+ if (ch === "}" || ch === "]" || ch === ",") {
427
+ // Not an error - just no value here (e.g., {"a":} or [,])
428
+ return undefined;
429
+ }
430
+
431
+ this.errors.push({
432
+ path,
433
+ expected: "JSON value",
434
+ value: this.getErrorContext(),
435
+ });
436
+ // Skip the problematic character and try to continue
437
+ this.pos++;
438
+ return undefined;
439
+ }
440
+
441
+ private getErrorContext(): string {
442
+ // Get surrounding context for better error messages
443
+ const start: number = Math.max(0, this.pos - 10);
444
+ const end: number = Math.min(this.input.length, this.pos + 20);
445
+ const before: string = this.input.slice(start, this.pos);
446
+ const after: string = this.input.slice(this.pos, end);
447
+ return (
448
+ (start > 0 ? "..." : "") +
449
+ before +
450
+ "→" +
451
+ after +
452
+ (end < this.input.length ? "..." : "")
453
+ );
454
+ }
455
+
456
+ private parseKeywordOrIdentifier(path: string): unknown {
457
+ // Extract the token (sequence of identifier characters)
458
+ const start: number = this.pos;
459
+ while (
460
+ this.pos < this.input.length &&
461
+ this.isIdentifierChar(this.input[this.pos]!)
462
+ ) {
463
+ this.pos++;
464
+ }
465
+ const token: string = this.input.slice(start, this.pos);
466
+
467
+ // Check for complete or partial keyword matches
468
+ if (token === "true") return true;
469
+ if (token === "false") return false;
470
+ if (token === "null") return null;
471
+
472
+ // Boolean string coercion: "yes", "y", "on" -> true, "no", "off" -> false
473
+ // Note: "n" is intentionally NOT handled (neither null nor false)
474
+ const lower: string = token.toLowerCase();
475
+ if (lower === "yes" || lower === "y" || lower === "on") return true;
476
+ if (lower === "no" || lower === "off") return false;
477
+
478
+ // Partial match for lenient parsing (e.g., "tru" -> true, "fal" -> false)
479
+ if ("true".startsWith(token) && token.length > 0) return true;
480
+ if ("false".startsWith(token) && token.length > 0) return false;
481
+ if ("null".startsWith(token) && token.length >= 2) return null;
482
+
483
+ // Check if this looks like a string with missing opening quote (e.g., abcdefg")
484
+ if (this.pos < this.input.length && this.input[this.pos] === '"') {
485
+ // Treat as unquoted string value - skip the errant closing quote and return as string
486
+ this.pos++; // skip the closing quote
487
+ this.errors.push({
488
+ path,
489
+ expected: "quoted string",
490
+ value: "missing opening quote for '" + token + "'",
491
+ });
492
+ return token;
493
+ }
494
+
495
+ // Invalid identifier as value - provide helpful error message
496
+ this.errors.push({
497
+ path,
498
+ expected: "JSON value (string, number, boolean, null, object, or array)",
499
+ value: "unquoted string '" + token + "' - did you forget quotes?",
500
+ });
501
+ // Skip to next comma, closing brace/bracket for recovery
502
+ this.skipToRecoveryPoint();
503
+ return undefined;
504
+ }
505
+
506
+ private skipToRecoveryPoint(): void {
507
+ while (this.pos < this.input.length) {
508
+ const ch: string = this.input[this.pos]!;
509
+ if (ch === "," || ch === "}" || ch === "]") {
510
+ return;
511
+ }
512
+ this.pos++;
513
+ }
514
+ }
515
+
516
+ private parseObject(path: string): Record<string, unknown> {
517
+ const result: Record<string, unknown> = {};
518
+ this.pos++; // skip '{'
519
+ this.depth++;
520
+ this.skipWhitespace();
521
+
522
+ while (this.pos < this.input.length) {
523
+ this.skipWhitespace();
524
+
525
+ // Handle end of object or end of input
526
+ if (this.pos >= this.input.length || this.input[this.pos] === "}") {
527
+ if (this.pos < this.input.length) this.pos++; // skip '}'
528
+ this.depth--;
529
+ return result;
530
+ }
531
+
532
+ // Skip trailing comma
533
+ if (this.input[this.pos] === ",") {
534
+ this.pos++;
535
+ this.skipWhitespace();
536
+ continue;
537
+ }
538
+
539
+ // Parse key (quoted string or unquoted identifier)
540
+ let key: string;
541
+ if (this.input[this.pos] === '"') {
542
+ key = this.parseString();
543
+ } else if (this.isIdentifierStart(this.input[this.pos]!)) {
544
+ key = this.parseIdentifier();
545
+ } else {
546
+ this.errors.push({
547
+ path,
548
+ expected: "string key",
549
+ value: this.input[this.pos],
550
+ });
551
+ // Try to recover by skipping to next meaningful character
552
+ this.depth--;
553
+ return result;
554
+ }
555
+ if (typeof key !== "string") {
556
+ this.depth--;
557
+ return result;
558
+ }
559
+
560
+ this.skipWhitespace();
561
+
562
+ // Expect colon - but if we're at end of input, it's just incomplete (not an error)
563
+ if (this.pos >= this.input.length) {
564
+ this.depth--;
565
+ return result;
566
+ }
567
+ if (this.input[this.pos] !== ":") {
568
+ this.errors.push({
569
+ path: path + "." + key,
570
+ expected: "':'",
571
+ value: this.input[this.pos],
572
+ });
573
+ this.depth--;
574
+ return result;
575
+ }
576
+ this.pos++; // skip ':'
577
+
578
+ this.skipWhitespace();
579
+
580
+ // Parse value
581
+ if (this.pos >= this.input.length) {
582
+ // No value - incomplete but not an error for lenient parsing
583
+ this.depth--;
584
+ return result;
585
+ }
586
+
587
+ const value: unknown = this.parseValue(path + "." + key);
588
+ result[key] = value;
589
+
590
+ this.skipWhitespace();
591
+
592
+ // Handle comma or end
593
+ if (this.pos < this.input.length && this.input[this.pos] === ",") {
594
+ this.pos++;
595
+ }
596
+ }
597
+
598
+ this.depth--;
599
+ return result;
600
+ }
601
+
602
+ private parseArray(path: string): unknown[] {
603
+ const result: unknown[] = [];
604
+ this.pos++; // skip '['
605
+ this.depth++;
606
+ this.skipWhitespace();
607
+
608
+ let index: number = 0;
609
+ while (this.pos < this.input.length) {
610
+ this.skipWhitespace();
611
+
612
+ // Handle end of array or end of input
613
+ if (this.pos >= this.input.length || this.input[this.pos] === "]") {
614
+ if (this.pos < this.input.length) this.pos++; // skip ']'
615
+ this.depth--;
616
+ return result;
617
+ }
618
+
619
+ // Skip trailing comma
620
+ if (this.input[this.pos] === ",") {
621
+ this.pos++;
622
+ this.skipWhitespace();
623
+ continue;
624
+ }
625
+
626
+ // Parse value
627
+ const prevPos: number = this.pos;
628
+ const value: unknown = this.parseValue(path + "[" + index + "]");
629
+
630
+ // Guard: if parseValue didn't advance, skip unexpected char to prevent infinite loop
631
+ if (this.pos === prevPos && this.pos < this.input.length) {
632
+ this.pos++;
633
+ continue;
634
+ }
635
+
636
+ result.push(value);
637
+ index++;
638
+
639
+ this.skipWhitespace();
640
+
641
+ // Handle comma or end
642
+ if (this.pos < this.input.length && this.input[this.pos] === ",") {
643
+ this.pos++;
644
+ }
645
+ }
646
+
647
+ this.depth--;
648
+ return result;
649
+ }
650
+
651
+ private parseString(): string {
652
+ this.pos++; // skip opening '"'
653
+ let result: string = "";
654
+ let escaped: boolean = false;
655
+
656
+ while (this.pos < this.input.length) {
657
+ const char: string = this.input[this.pos]!;
658
+
659
+ if (escaped) {
660
+ switch (char) {
661
+ case '"':
662
+ result += '"';
663
+ break;
664
+ case "\\":
665
+ result += "\\";
666
+ break;
667
+ case "/":
668
+ result += "/";
669
+ break;
670
+ case "b":
671
+ result += "\b";
672
+ break;
673
+ case "f":
674
+ result += "\f";
675
+ break;
676
+ case "n":
677
+ result += "\n";
678
+ break;
679
+ case "r":
680
+ result += "\r";
681
+ break;
682
+ case "t":
683
+ result += "\t";
684
+ break;
685
+ case "u":
686
+ // Parse unicode escape
687
+ if (this.pos + 4 <= this.input.length) {
688
+ const hex: string = this.input.slice(this.pos + 1, this.pos + 5);
689
+ if (isHexString(hex)) {
690
+ const highCode: number = parseInt(hex, 16);
691
+ this.pos += 4;
692
+
693
+ // Check for surrogate pair (emoji and characters > U+FFFF)
694
+ if (
695
+ highCode >= 0xd800 &&
696
+ highCode <= 0xdbff &&
697
+ this.pos + 6 <= this.input.length &&
698
+ this.input[this.pos + 1] === "\\" &&
699
+ this.input[this.pos + 2] === "u"
700
+ ) {
701
+ const lowHex: string = this.input.slice(
702
+ this.pos + 3,
703
+ this.pos + 7,
704
+ );
705
+ if (isHexString(lowHex)) {
706
+ const lowCode: number = parseInt(lowHex, 16);
707
+ if (lowCode >= 0xdc00 && lowCode <= 0xdfff) {
708
+ result += String.fromCharCode(highCode, lowCode);
709
+ this.pos += 6;
710
+ break;
711
+ }
712
+ }
713
+ }
714
+ result += String.fromCharCode(highCode);
715
+ } else {
716
+ // Invalid hex - preserve escape sequence literally
717
+ result += "\\u" + hex;
718
+ this.pos += 4;
719
+ }
720
+ } else {
721
+ // Incomplete unicode escape - add partial sequence
722
+ const partial: string = this.input.slice(this.pos + 1);
723
+ result += "\\u" + partial;
724
+ this.pos = this.input.length - 1;
725
+ }
726
+ break;
727
+ default:
728
+ result += char;
729
+ }
730
+ escaped = false;
731
+ this.pos++;
732
+ continue;
733
+ }
734
+
735
+ if (char === "\\") {
736
+ escaped = true;
737
+ this.pos++;
738
+ continue;
739
+ }
740
+
741
+ if (char === '"') {
742
+ this.pos++; // skip closing '"'
743
+ return result;
744
+ }
745
+
746
+ result += char;
747
+ this.pos++;
748
+ }
749
+
750
+ // Unclosed string - return what we have (lenient)
751
+ return result;
752
+ }
753
+
754
+ private parseNumber(): number {
755
+ const start: number = this.pos;
756
+
757
+ // Handle negative sign
758
+ if (this.input[this.pos] === "-") {
759
+ this.pos++;
760
+ }
761
+
762
+ // Parse integer part
763
+ while (
764
+ this.pos < this.input.length &&
765
+ this.input[this.pos]! >= "0" &&
766
+ this.input[this.pos]! <= "9"
767
+ ) {
768
+ this.pos++;
769
+ }
770
+
771
+ // Parse decimal part
772
+ if (this.pos < this.input.length && this.input[this.pos] === ".") {
773
+ this.pos++;
774
+ while (
775
+ this.pos < this.input.length &&
776
+ this.input[this.pos]! >= "0" &&
777
+ this.input[this.pos]! <= "9"
778
+ ) {
779
+ this.pos++;
780
+ }
781
+ }
782
+
783
+ // Parse exponent
784
+ if (
785
+ this.pos < this.input.length &&
786
+ (this.input[this.pos] === "e" || this.input[this.pos] === "E")
787
+ ) {
788
+ this.pos++;
789
+ if (
790
+ this.pos < this.input.length &&
791
+ (this.input[this.pos] === "+" || this.input[this.pos] === "-")
792
+ ) {
793
+ this.pos++;
794
+ }
795
+ while (
796
+ this.pos < this.input.length &&
797
+ this.input[this.pos]! >= "0" &&
798
+ this.input[this.pos]! <= "9"
799
+ ) {
800
+ this.pos++;
801
+ }
802
+ }
803
+
804
+ const numStr: string = this.input.slice(start, this.pos);
805
+ const num: number = Number(numStr);
806
+ return Number.isNaN(num) ? 0 : num;
807
+ }
808
+
809
+ private isIdentifierStart(ch: string): boolean {
810
+ return (
811
+ (ch >= "a" && ch <= "z") ||
812
+ (ch >= "A" && ch <= "Z") ||
813
+ ch === "_" ||
814
+ ch === "$"
815
+ );
816
+ }
817
+
818
+ private isIdentifierChar(ch: string): boolean {
819
+ return (
820
+ (ch >= "a" && ch <= "z") ||
821
+ (ch >= "A" && ch <= "Z") ||
822
+ (ch >= "0" && ch <= "9") ||
823
+ ch === "_" ||
824
+ ch === "$"
825
+ );
826
+ }
827
+
828
+ private parseIdentifier(): string {
829
+ const start: number = this.pos;
830
+ while (
831
+ this.pos < this.input.length &&
832
+ this.isIdentifierChar(this.input[this.pos]!)
833
+ ) {
834
+ this.pos++;
835
+ }
836
+ return this.input.slice(start, this.pos);
837
+ }
838
+
839
+ private skipWhitespace(): void {
840
+ while (this.pos < this.input.length) {
841
+ const ch: string = this.input[this.pos]!;
842
+
843
+ // Skip standard whitespace
844
+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
845
+ this.pos++;
846
+ continue;
847
+ }
848
+
849
+ // Skip single-line comment: // ...
850
+ if (
851
+ ch === "/" &&
852
+ this.pos + 1 < this.input.length &&
853
+ this.input[this.pos + 1] === "/"
854
+ ) {
855
+ this.pos += 2;
856
+ while (
857
+ this.pos < this.input.length &&
858
+ this.input[this.pos] !== "\n" &&
859
+ this.input[this.pos] !== "\r"
860
+ ) {
861
+ this.pos++;
862
+ }
863
+ continue;
864
+ }
865
+
866
+ // Skip multi-line comment: /* ... */
867
+ if (
868
+ ch === "/" &&
869
+ this.pos + 1 < this.input.length &&
870
+ this.input[this.pos + 1] === "*"
871
+ ) {
872
+ this.pos += 2;
873
+ while (this.pos + 1 < this.input.length) {
874
+ if (
875
+ this.input[this.pos] === "*" &&
876
+ this.input[this.pos + 1] === "/"
877
+ ) {
878
+ this.pos += 2;
879
+ break;
880
+ }
881
+ this.pos++;
882
+ }
883
+ // Handle unclosed comment - move to end
884
+ if (this.pos + 1 >= this.input.length) {
885
+ this.pos = this.input.length;
886
+ }
887
+ continue;
888
+ }
889
+
890
+ // Not whitespace or comment
891
+ break;
892
+ }
893
+ }
894
+ }