(filePath: string)
| 5 | * Parse JSON files |
| 6 | */ |
| 7 | export async function parseJSON(filePath: string): Promise<FileParseResult> { |
| 8 | const fs = await import('fs/promises') |
| 9 | const content = await fs.readFile(filePath, 'utf-8') |
| 10 | |
| 11 | try { |
| 12 | // Parse to validate JSON |
| 13 | const jsonData = JSON.parse(content) |
| 14 | |
| 15 | // Return pretty-printed JSON for better readability |
| 16 | const formattedContent = JSON.stringify(jsonData, null, 2) |
| 17 | |
| 18 | // Extract metadata about the JSON structure |
| 19 | const metadata = { |
| 20 | type: 'json', |
| 21 | isArray: Array.isArray(jsonData), |
| 22 | keys: Array.isArray(jsonData) ? [] : Object.keys(jsonData), |
| 23 | itemCount: Array.isArray(jsonData) ? jsonData.length : undefined, |
| 24 | depth: getJsonDepth(jsonData), |
| 25 | } |
| 26 | |
| 27 | return { |
| 28 | content: formattedContent, |
| 29 | metadata, |
| 30 | } |
| 31 | } catch (error) { |
| 32 | throw new Error(`Invalid JSON: ${getErrorMessage(error, 'Unknown error')}`) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Parse JSON from buffer |
nothing calls this directly
no test coverage detected