diff --git a/src/tools/json-viewer/json.models.test.ts b/src/tools/json-viewer/json.models.test.ts index e0172356..6d4ab335 100644 --- a/src/tools/json-viewer/json.models.test.ts +++ b/src/tools/json-viewer/json.models.test.ts @@ -83,6 +83,13 @@ describe('json models', () => { expect(result).toBe(expected); }); + it('does not unescape an escaped backslash twice', () => { + const escapedJson = '{\\"text\\":\\"line1\\\\nline2\\",\\"path\\":\\"C:\\\\\\\\new\\"}'; + const result = formatJson({ rawJson: escapedJson, unescapeJsonString: true, indentSize: 2 }); + const expected = '{\n "path": "C:\\\\new",\n "text": "line1\\nline2"\n}'; + expect(result).toBe(expected); + }); + it('handles single-quoted outer strings', () => { const escapedJson = '\'{\\\"id\\\":\\\"123\\\"}\''; const result = formatJson({ rawJson: escapedJson, unescapeJsonString: true, indentSize: 2 }); diff --git a/src/tools/json-viewer/json.models.ts b/src/tools/json-viewer/json.models.ts index 704e865e..157c6f57 100644 --- a/src/tools/json-viewer/json.models.ts +++ b/src/tools/json-viewer/json.models.ts @@ -28,6 +28,17 @@ function unescapeUnicodeJSON(str: string) { return str.replace(/\\u([\dA-Fa-f]{4})/g, (match, grp) => String.fromCharCode(Number.parseInt(grp, 16))); } +const JSON_STRING_ESCAPES: Record = { + '"': '"', + '\\': '\\', + '/': '/', + n: '\n', + r: '\r', + t: '\t', + f: '\f', + b: '\b', +}; + function unescapeJson(jsonString: string): string { try { // First, try to handle double-escaped scenarios @@ -40,16 +51,9 @@ function unescapeJson(jsonString: string): string { result = result.slice(1, -1); } - // Handle common escape sequences - result = result - .replace(/\\"/g, '"') // Unescape quotes - .replace(/\\\\/g, '\\') // Unescape backslashes (do this after quotes!) - .replace(/\\n/g, '\n') // Unescape newlines - .replace(/\\r/g, '\r') // Unescape carriage returns - .replace(/\\t/g, '\t') // Unescape tabs - .replace(/\\f/g, '\f') // Unescape form feeds - .replace(/\\b/g, '\b') // Unescape backspaces - .replace(/\\\//g, '/'); // Unescape forward slashes + // Handle common escape sequences in a single pass, so an escaped backslash + // is not unescaped twice: `\\\\n` becomes `\\n`, not a newline + result = result.replace(/\\(["\\/nrtfb])/g, (_, char: string) => JSON_STRING_ESCAPES[char]); return result; } catch {