@@ -355,7 +355,7 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
355355 })?;
356356
357357 if let Some(output_path) = output_path {
358- write_result_to_file_as_json(&exercises, output_path)?;
358+ write_result_to_file_as_json(&exercises, output_path, pretty )?;
359359 }
360360
361361 let output = Output::OutputData(OutputData {
@@ -383,7 +383,7 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
383383 })?;
384384
385385 if let Some(output_path) = output_path {
386- write_result_to_file_as_json(&config, output_path)?;
386+ write_result_to_file_as_json(&config, output_path, pretty )?;
387387 }
388388
389389 let output = Output::OutputData(OutputData {
@@ -669,7 +669,7 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
669669 };
670670
671671 if let Some(output_path) = output_path {
672- write_result_to_file_as_json(&test_result, output_path)?;
672+ write_result_to_file_as_json(&test_result, output_path, pretty )?;
673673 }
674674
675675 // todo: checkstyle results in stdout?
@@ -717,7 +717,7 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
717717 })?;
718718
719719 if let Some(output_path) = output_path {
720- write_result_to_file_as_json(&scan_result, output_path)?;
720+ write_result_to_file_as_json(&scan_result, output_path, pretty )?;
721721 }
722722
723723 let output = Output::OutputData(OutputData {
@@ -1660,20 +1660,33 @@ fn print_warnings(pretty: bool, warnings: &[anyhow::Error]) -> Result<()> {
16601660 Ok(())
16611661}
16621662
1663- fn write_result_to_file_as_json<T: Serialize>(result: &T, output_path: &Path) -> Result<()> {
1663+ fn write_result_to_file_as_json<T: Serialize>(
1664+ result: &T,
1665+ output_path: &Path,
1666+ pretty: bool,
1667+ ) -> Result<()> {
16641668 let output_file = File::create(output_path).with_context(|| {
16651669 format!(
16661670 "Failed to create results JSON file at {}",
16671671 output_path.display()
16681672 )
16691673 })?;
16701674
1671- serde_json::to_writer(output_file, result).with_context(|| {
1672- format!(
1673- "Failed to write result as JSON to {}",
1674- output_path.display()
1675- )
1676- })?;
1675+ if pretty {
1676+ serde_json::to_writer_pretty(output_file, result).with_context(|| {
1677+ format!(
1678+ "Failed to write result as JSON to {}",
1679+ output_path.display()
1680+ )
1681+ })?;
1682+ } else {
1683+ serde_json::to_writer(output_file, result).with_context(|| {
1684+ format!(
1685+ "Failed to write result as JSON to {}",
1686+ output_path.display()
1687+ )
1688+ })?;
1689+ }
16771690
16781691 Ok(())
16791692}
0 commit comments