|
|
@@ -211,6 +211,7 @@ function extractBalancedJsonObject(text: string): string | null {
|
|
|
let depth = 0;
|
|
|
let inString = false;
|
|
|
let escaped = false;
|
|
|
+ let endIndex = -1;
|
|
|
|
|
|
for (let index = start; index < text.length; index += 1) {
|
|
|
const char = text[index]!;
|
|
|
@@ -243,7 +244,8 @@ function extractBalancedJsonObject(text: string): string | null {
|
|
|
if (char === "}") {
|
|
|
depth -= 1;
|
|
|
if (depth === 0) {
|
|
|
- return text.slice(start, index + 1);
|
|
|
+ endIndex = index;
|
|
|
+ break;
|
|
|
}
|
|
|
if (depth < 0) {
|
|
|
return null;
|
|
|
@@ -251,5 +253,24 @@ function extractBalancedJsonObject(text: string): string | null {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
+ if (endIndex < 0) return null;
|
|
|
+
|
|
|
+ // Only accept the candidate if what follows the closing brace is
|
|
|
+ // nothing, whitespace, or a structural JSON terminator.
|
|
|
+ // This rejects trailing content like "{...} more text here"
|
|
|
+ const followingChar = text[endIndex + 1];
|
|
|
+ if (
|
|
|
+ followingChar !== undefined &&
|
|
|
+ followingChar !== "\n" &&
|
|
|
+ followingChar !== "\r" &&
|
|
|
+ followingChar !== "\t" &&
|
|
|
+ followingChar !== " " &&
|
|
|
+ followingChar !== "," &&
|
|
|
+ followingChar !== "]" &&
|
|
|
+ followingChar !== "}"
|
|
|
+ ) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return text.slice(start, endIndex + 1);
|
|
|
}
|