(buffer: Buffer)
| 37 | * Parse JSON from buffer |
| 38 | */ |
| 39 | export async function parseJSONBuffer(buffer: Buffer): Promise<FileParseResult> { |
| 40 | const content = buffer.toString('utf-8') |
| 41 | |
| 42 | try { |
| 43 | const jsonData = JSON.parse(content) |
| 44 | const formattedContent = JSON.stringify(jsonData, null, 2) |
| 45 | |
| 46 | const metadata = { |
| 47 | type: 'json', |
| 48 | isArray: Array.isArray(jsonData), |
| 49 | keys: Array.isArray(jsonData) ? [] : Object.keys(jsonData), |
| 50 | itemCount: Array.isArray(jsonData) ? jsonData.length : undefined, |
| 51 | depth: getJsonDepth(jsonData), |
| 52 | } |
| 53 | |
| 54 | return { |
| 55 | content: formattedContent, |
| 56 | metadata, |
| 57 | } |
| 58 | } catch (error) { |
| 59 | throw new Error(`Invalid JSON: ${getErrorMessage(error, 'Unknown error')}`) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Parse JSONL (JSON Lines) files — one JSON object per line |
nothing calls this directly
no test coverage detected