FormatValidationJSON converts validation results to JSON format. Generates structured JSON output from validation results, suitable for programmatic consumption, CI/CD integration, and automated processing. Parameters: - result: Validation results to format - inputFiles: Array of input file paths
(result *ValidationResult, inputFiles []string, includeStats bool)
| 264 | // |
| 265 | // FormatValidationJSON converts validation results to JSON format |
| 266 | func FormatValidationJSON(result *ValidationResult, inputFiles []string, includeStats bool) ([]byte, error) { |
| 267 | output := &JSONValidationOutput{ |
| 268 | Command: "validate", |
| 269 | Input: JSONInputInfo{ |
| 270 | Type: determineInputType(inputFiles), |
| 271 | Files: inputFiles, |
| 272 | Count: len(inputFiles), |
| 273 | }, |
| 274 | Status: determineStatus(result), |
| 275 | Results: JSONValidationResults{ |
| 276 | Valid: result.InvalidFiles == 0, |
| 277 | TotalFiles: result.TotalFiles, |
| 278 | ValidFiles: result.ValidFiles, |
| 279 | InvalidFiles: result.InvalidFiles, |
| 280 | }, |
| 281 | Errors: make([]JSONValidationError, 0), |
| 282 | } |
| 283 | |
| 284 | // Add errors |
| 285 | for _, fileResult := range result.Files { |
| 286 | if fileResult.Error != nil { |
| 287 | errCode := extractErrorCode(fileResult.Error) |
| 288 | output.Errors = append(output.Errors, JSONValidationError{ |
| 289 | File: fileResult.Path, |
| 290 | Message: fileResult.Error.Error(), |
| 291 | Code: errCode, |
| 292 | Type: categorizeByCode(errCode, fileResult.Error.Error()), |
| 293 | }) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Add statistics if requested |
| 298 | if includeStats { |
| 299 | throughputFPS := 0.0 |
| 300 | if result.Duration.Seconds() > 0 { |
| 301 | throughputFPS = float64(result.TotalFiles) / result.Duration.Seconds() |
| 302 | } |
| 303 | |
| 304 | throughputBPS := int64(0) |
| 305 | if result.Duration.Seconds() > 0 { |
| 306 | throughputBPS = int64(float64(result.TotalBytes) / result.Duration.Seconds()) |
| 307 | } |
| 308 | |
| 309 | output.Stats = &JSONValidationStats{ |
| 310 | Duration: result.Duration.String(), |
| 311 | DurationMs: float64(result.Duration.Milliseconds()), |
| 312 | TotalBytes: result.TotalBytes, |
| 313 | ThroughputFPS: throughputFPS, |
| 314 | ThroughputBPS: throughputBPS, |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Marshal to JSON with indentation |
| 319 | data, err := json.MarshalIndent(output, "", " ") |
| 320 | if err != nil { |
| 321 | return nil, fmt.Errorf("failed to marshal validation JSON: %w", err) |
| 322 | } |
| 323 |