Skip to content

Conversation

@JamieMagee
Copy link
Member

@JamieMagee JamieMagee commented Nov 25, 2025

Convert FileWritingService, ScanCommand, Mariner2ArtifactFilter, and DetectorProcessingService to use System.Text.Json for serialization.

LinuxScanner retained on Newtonsoft for now due to SyftOutput union types requiring custom converters.

….Json

Convert `FileWritingService`, `ScanCommand`, Mariner2ArtifactFilter, and
`DetectorProcessingService` to use `System.Text.Json` for serialization.

`LinuxScanner` retained on Newtonsoft for now due to `SyftOutput` union types
requiring custom converters.
@codecov
Copy link

codecov bot commented Nov 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.1%. Comparing base (59ab9db) to head (788c53e).

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1566     +/-   ##
=======================================
- Coverage   90.1%   90.1%   -0.1%     
=======================================
  Files        426     426             
  Lines      35981   35975      -6     
  Branches    2221    2221             
=======================================
- Hits       32442   32435      -7     
- Misses      3079    3080      +1     
  Partials     460     460             

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot finished reviewing on behalf of JamieMagee November 25, 2025 04:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates JSON serialization from Newtonsoft.Json to System.Text.Json for four files: FileWritingService, ScanCommand, Mariner2ArtifactFilter, and DetectorProcessingService. The migration focuses on straightforward serialization scenarios, while complex use cases (like LinuxScanner with union types) remain on Newtonsoft.Json. The changes maintain backward compatibility by keeping model classes dual-annotated with both Newtonsoft.Json and System.Text.Json attributes during the transition.

Key Changes

  • Replaced direct JsonSerializer and JsonTextWriter usage with System.Text.Json.JsonSerializer static methods
  • Added JsonSerializerOptions configuration with JavaScriptEncoder.UnsafeRelaxedJsonEscaping and WriteIndented settings in FileWritingService
  • Updated imports: removed using Newtonsoft.Json; and added using System.Text.Json; (and System.Text.Encodings.Web where needed)

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Microsoft.ComponentDetection.Orchestrator/Services/DetectorProcessingService.cs Updated telemetry serialization to use System.Text.Json
src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs Migrated console output serialization to System.Text.Json with indented formatting
src/Microsoft.ComponentDetection.Detectors/linux/Filters/Mariner2ArtifactFilter.cs Updated telemetry record serialization to System.Text.Json
src/Microsoft.ComponentDetection.Common/FileWritingService.cs Refactored file writing methods to use System.Text.Json with custom serialization options including relaxed JSON escaping

public sealed class ScanCommand : AsyncCommand<ScanSettings>
{
private const string ManifestRelativePath = "ScanManifest_{timestamp}.json";
private static readonly JsonSerializerOptions IndentedJsonOptions = new() { WriteIndented = true };
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IndentedJsonOptions used for console output (line 94) doesn't include JavaScriptEncoder.UnsafeRelaxedJsonEscaping, while the FileWritingService uses it (FileWritingService.cs:24). This creates inconsistent JSON formatting between console and file output - the console output will have more aggressively escaped characters (e.g., + becomes \u002B). Consider adding Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping to maintain consistency with the file output format.

Copilot uses AI. Check for mistakes.
containerDetails = result.ContainerDetails;

record.AdditionalTelemetryDetails = result.AdditionalTelemetryDetails != null ? JsonConvert.SerializeObject(result.AdditionalTelemetryDetails) : null;
record.AdditionalTelemetryDetails = result.AdditionalTelemetryDetails != null ? JsonSerializer.Serialize(result.AdditionalTelemetryDetails) : null;
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JsonSerializer.Serialize call doesn't specify any JsonSerializerOptions, which means it uses default serialization settings. This will produce more aggressively escaped JSON compared to Newtonsoft.Json's default behavior. For consistency with other parts of the codebase (e.g., FileWritingService), consider adding a static JsonSerializerOptions field with Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping to maintain similar output formatting to the previous Newtonsoft.Json implementation.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings November 25, 2025 05:12
@JamieMagee JamieMagee force-pushed the users/jamagee/migrate-simple-json-to-stj branch from 58d9a84 to 9fd0bfc Compare November 25, 2025 05:12
Copilot finished reviewing on behalf of JamieMagee November 25, 2025 05:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

[System.Text.Json.Serialization.JsonIgnore] // System.Text.Json - type is handled by [JsonPolymorphic] discriminator
public abstract ComponentType Type { get; }

/// <summary>Gets the id of the component.</summary>
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Id property is missing the System.Text.Json attribute [JsonPropertyName(\"id\")]. While the Newtonsoft.Json attribute is present, the corresponding System.Text.Json attribute is required for serialization to work correctly with System.Text.Json. This will cause the Id property to not be serialized when using System.Text.Json. Add [JsonPropertyName(\"id\")] on line 57 before the existing Newtonsoft.Json attribute, following the dual-attribute pattern used elsewhere in the codebase.

Suggested change
/// <summary>Gets the id of the component.</summary>
/// <summary>Gets the id of the component.</summary>
[JsonPropertyName("id")]

Copilot uses AI. Check for mistakes.
@JamieMagee JamieMagee force-pushed the users/jamagee/migrate-simple-json-to-stj branch 2 times, most recently from 868c94e to dc179eb Compare November 25, 2025 05:30
Copilot AI review requested due to automatic review settings November 25, 2025 05:30
Copilot finished reviewing on behalf of JamieMagee November 25, 2025 05:34
@JamieMagee JamieMagee force-pushed the users/jamagee/migrate-simple-json-to-stj branch from dc179eb to 788c53e Compare November 25, 2025 05:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants