From 7b8e8fc43ee8132050bf49489c418ca706c4566c Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Fri, 29 Mar 2024 09:15:20 -0700 Subject: [PATCH] Fix newline starting new object with missing colon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • detect if the current string we are inside should become a key and add missing closing quote mark and colon if so • add a test to verify it --- packages/parsing/src/parse-as-json.test.ts | 51 ++++++++++++++++++++++ packages/parsing/src/parse-as-json.ts | 3 ++ 2 files changed, 54 insertions(+) diff --git a/packages/parsing/src/parse-as-json.test.ts b/packages/parsing/src/parse-as-json.test.ts index 356caa95..37cd4bd7 100644 --- a/packages/parsing/src/parse-as-json.test.ts +++ b/packages/parsing/src/parse-as-json.test.ts @@ -356,6 +356,57 @@ Props: // first quote mark is recognized as an unescaped character, final quote mark is treated as string terminus teamMemberJobTitle1: 'Product Here is the JSON output for the "Testimonials', }); + + response = `\ +\`\`\`json +{"items":[{"heading":"Welcome to Lava","subheading":"Your One-Stop Shop for Lava Lamps and Whisky Ice Cream","description":"Lava is a unique store located in the heart of Noe Valley, San Francisco. We specialize in selling a wide variety of lava lamps, from classic designs to modern and quirky creations. But that's not all! We also offer a delicious treat that's sure to tantalize your taste buds: free whisky ice cream for those who come in before noon. Come visit us and experience the Lava magic for yourself!","\`\`\`json +{ + "heading": "Lava Lamps", + "subheading": "Choose from our wide selection of lava lamps in different sizes, colors, and features.", + "items": [ + { + "heading": "Classic Lava Lamp", + "price": "$39.99", + "button": "Add to Cart", + "imageURL": "/classic-lava-lamp.jpg", + "imageAlt": "Classic Lava Lamp" + }, + { + "heading": "Glowing Lava Lamp", + "price": "$44.99", + "button": "Add to Cart", + "imageURL": "/`; + + expect(parseAsJSON(response)).toEqual({ + items: [ + { + heading: 'Welcome to Lava', + subheading: 'Your One-Stop Shop for Lava Lamps and Whisky Ice Cream', + description: + "Lava is a unique store located in the heart of Noe Valley, San Francisco. We specialize in selling a wide variety of lava lamps, from classic designs to modern and quirky creations. But that's not all! We also offer a delicious treat that's sure to tantalize your taste buds: free whisky ice cream for those who come in before noon. Come visit us and experience the Lava magic for yourself!", + '```json': { + heading: 'Lava Lamps', + subheading: + 'Choose from our wide selection of lava lamps in different sizes, colors, and features.', + items: [ + { + heading: 'Classic Lava Lamp', + price: '$39.99', + button: 'Add to Cart', + imageURL: '/classic-lava-lamp.jpg', + imageAlt: 'Classic Lava Lamp', + }, + { + heading: 'Glowing Lava Lamp', + price: '$44.99', + button: 'Add to Cart', + imageURL: '/', + }, + ], + }, + }, + ], + }); } function preambleTestCase() { diff --git a/packages/parsing/src/parse-as-json.ts b/packages/parsing/src/parse-as-json.ts index de0cc7a3..553d1445 100644 --- a/packages/parsing/src/parse-as-json.ts +++ b/packages/parsing/src/parse-as-json.ts @@ -315,6 +315,9 @@ export function parseAsJSON(text: string): ParsedValue | null { newText.substring(keyStartIndex, lastColonIndex + 1) + '"' + newText.substring(lastColonIndex + 1); + } else if (getPreviousStringType(newText) === 'VALUE') { + // if previous string is a value, convert current string into a key + char = '":'; } else { // if no key was found, add one let keyIndex = 1;