diff --git a/cypress/components/JsonParserReproduction.cy.tsx b/cypress/components/JsonParserReproduction.cy.tsx new file mode 100644 index 000000000..c7dafb1d5 --- /dev/null +++ b/cypress/components/JsonParserReproduction.cy.tsx @@ -0,0 +1,22 @@ +import getPartsOfJson from '../../lib/getPartsOfJson'; + +describe('JsonParserReproduction', () => { + it('should correctly parse JSON with escaped backslash at the end of a string', () => { + const problemJson = + '{ "bad_string": "backslash: \\\\", "next_key": "this should be found" }'; + + const parts = getPartsOfJson(problemJson); + + // Check if 'next_key' is identified as an object property + const nextKeyPart = parts.find( + (p) => p.match === 'next_key' && p.type === 'objectProperty', + ); + + // Log parts for debugging if it fails + if (!nextKeyPart) { + console.log('Parsed parts:', JSON.stringify(parts, null, 2)); + } + + expect(nextKeyPart).to.not.be.undefined; + }); +}); diff --git a/lib/getPartsOfJson.ts b/lib/getPartsOfJson.ts index d8b741ccb..6d9ef7991 100644 --- a/lib/getPartsOfJson.ts +++ b/lib/getPartsOfJson.ts @@ -6,10 +6,10 @@ const regexObject = const regexArray = /^\s*(?\[)(?.*)(?\])\s*$/g; const regexNumber = /^\s*(?-?\d+(\.\d+)?([Ee][+-]?\d+)?)\s*$/g; -const regexString = /^\s*(?"(\\"|[^"])*")\s*$/g; +const regexString = /^\s*(?"(?:[^"\\]|\\.)*")\s*$/g; const regexBoolean = /^\s*(?true|false)\s*$/g; const regexNull = /^\s*(?null)\s*$/g; -const regexDoubleQuote = /(?