|
1 | 1 | import logger from '@/utils/logger';
|
2 | 2 |
|
3 | 3 | import losslessJsonParse from '../lossless-json-parse';
|
| 4 | + |
| 5 | +const separators = ['\n', ' ']; |
| 6 | + |
4 | 7 | const formatInputPayload = (
|
5 | 8 | payload: { data?: string | null } | null | undefined
|
6 | 9 | ) => {
|
7 | 10 | const data = payload?.data;
|
8 |
| - |
9 | 11 | if (!data) {
|
10 | 12 | return null;
|
11 | 13 | }
|
12 | 14 |
|
13 | 15 | const parsedData = atob(data);
|
14 |
| - return parseJsonLines(parsedData); |
| 16 | + return parseMultipleInputs(parsedData); |
15 | 17 | };
|
16 | 18 |
|
17 |
| -function parseJsonLines(input: string) { |
18 |
| - const jsonArray = []; |
19 |
| - let currentJson = ''; |
20 |
| - const separators = ['\n', ' ']; |
21 |
| - |
22 |
| - for (let i = 0; i < input.length; i++) { |
23 |
| - const char = input[i]; |
24 |
| - if (separators.includes(char)) { |
25 |
| - // Try to parse the current JSON string |
26 |
| - if (currentJson) { |
27 |
| - try { |
28 |
| - const jsonObject = losslessJsonParse(currentJson); |
29 |
| - // If successful, add the object to the array |
30 |
| - jsonArray.push(jsonObject); |
31 |
| - // Reset currentJson for the next JSON object |
32 |
| - currentJson = ''; |
33 |
| - } catch { |
34 |
| - // If parsing fails, treat the separator as part of the currentJson and continue with the next char |
35 |
| - currentJson += char; |
36 |
| - } |
37 |
| - } |
38 |
| - } else { |
39 |
| - currentJson += char; |
| 19 | +const parseMultipleInputs = (input: string) => { |
| 20 | + const results = []; |
| 21 | + let currentIndex = 0; |
| 22 | + |
| 23 | + while (currentIndex < input.length) { |
| 24 | + while (separators.includes(input[currentIndex])) { |
| 25 | + currentIndex++; |
40 | 26 | }
|
41 |
| - } |
| 27 | + if (currentIndex >= input.length) break; |
42 | 28 |
|
43 |
| - // Handle case where the last JSON object might be malformed |
44 |
| - if (currentJson.trim() !== '') { |
45 | 29 | try {
|
46 |
| - const jsonObject = losslessJsonParse(currentJson); |
47 |
| - jsonArray.push(jsonObject); |
48 |
| - } catch { |
| 30 | + const { startIndex, endIndex, jsonString } = extractJsonValue( |
| 31 | + input, |
| 32 | + currentIndex |
| 33 | + ); |
| 34 | + |
| 35 | + if (endIndex > startIndex) { |
| 36 | + // move to the end of the string before parsing to avoid parsing the same string if the parsing fails |
| 37 | + currentIndex = endIndex; |
| 38 | + results.push(losslessJsonParse(jsonString)); |
| 39 | + } else { |
| 40 | + currentIndex++; |
| 41 | + } |
| 42 | + } catch (error) { |
49 | 43 | logger.error(
|
50 | 44 | {
|
51 | 45 | input,
|
52 |
| - currentJson, |
| 46 | + currentIndex, |
| 47 | + error, |
53 | 48 | },
|
54 | 49 | 'Error parsing JSON string'
|
55 | 50 | );
|
56 | 51 | }
|
57 | 52 | }
|
58 | 53 |
|
59 |
| - return jsonArray; |
60 |
| -} |
| 54 | + return results; |
| 55 | +}; |
| 56 | + |
| 57 | +const extractJsonValue = (input: string, startIndex: number) => { |
| 58 | + let endIndex = startIndex; |
| 59 | + let openBrackets = 0; |
| 60 | + let openBraces = 0; |
| 61 | + let inString = false; |
| 62 | + let escapeNext = false; // used to handle escaped quotes |
| 63 | + |
| 64 | + while (endIndex < input.length) { |
| 65 | + const char = input[endIndex]; |
61 | 66 |
|
| 67 | + if (escapeNext) { |
| 68 | + escapeNext = false; |
| 69 | + } else if (char === '\\') { |
| 70 | + escapeNext = true; |
| 71 | + } else if (char === '"' && !inString) { |
| 72 | + inString = true; |
| 73 | + } else if (char === '"' && inString) { |
| 74 | + inString = false; |
| 75 | + } else if (!inString) { |
| 76 | + if (char === '[') openBrackets++; |
| 77 | + if (char === ']') openBrackets--; |
| 78 | + if (char === '{') openBraces++; |
| 79 | + if (char === '}') openBraces--; |
| 80 | + |
| 81 | + // When a separator is found, this indicates the end of a JSON value if we are not inside any array or object |
| 82 | + if (separators.includes(char) && openBrackets === 0 && openBraces === 0) { |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + endIndex++; |
| 87 | + } |
| 88 | + |
| 89 | + return { |
| 90 | + startIndex, |
| 91 | + endIndex, |
| 92 | + jsonString: input.slice(startIndex, endIndex), |
| 93 | + }; |
| 94 | +}; |
62 | 95 | export default formatInputPayload;
|
0 commit comments