(text: string)
| 158 | } |
| 159 | |
| 160 | export function isJson(text: string): boolean { |
| 161 | const possibleJsonChunk = getPossibleJsonChunk(text); |
| 162 | if (!possibleJsonChunk) { |
| 163 | console.log(`No possible JSON chunk found in:\n${text}`); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | const commentLessJson = possibleJsonChunk.replace( |
| 168 | /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, |
| 169 | (m, g) => (g ? "" : m), |
| 170 | ); |
| 171 | |
| 172 | try { |
| 173 | JSON.parse(commentLessJson); |
| 174 | return true; |
| 175 | } catch { |
| 176 | try { |
| 177 | // Try to see if outer brackets are missing |
| 178 | JSON.parse(`[${commentLessJson}]`); |
| 179 | return true; |
| 180 | } catch { |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | export function unprettifyJsonString(jsonString: string): string { |
| 187 | // Convert all comments to /* */ style |
no test coverage detected