Skip to content

[0.16] --json files are written with UTF-8 BOM #3070

@martincostello

Description

@martincostello

Trying to run some benchmarks for an application target .NET 11 using the latest CI build (0.16.0-nightly.20260331.487), trying to read the file contents output via --json in a JavaScript GitHub Action parsing the JSON is failing.

Error: Error: Failed to parse '/home/runner/work/costellobot/costellobot/BenchmarkDotNet.Artifacts/results/MartinCostello.Costellobot.Benchmarks.AppBenchmarks-report-full-compressed.json' as BenchmarkDotNet JSON output. Results must be a JSON file generated with the '--exporters json' option: SyntaxError: Unexpected token '', "{"Title":"... is not valid JSON

This is what the code in my action does:

const json = await fs.promises.readFile(fileName, { encoding: 'utf8' });
return JSON.parse(json);

After some debugging and pasting the content into https://repljs.com/ I spotted this:

Image

Looks like the refactoring in #2970 to remove SimpleJson has changed the default encoding to UTF-8 with a Byte Order Mark.

I'm guessing this is an unintentional change caused by the System.Text.Json defaults?

private static readonly JsonSerializerOptions DefaultOptions = new()
{
Converters =
{
new SimpleJsonFloatConverter(),
new SimpleJsonDoubleConverter(),
},
// Disable escaping non ASCII chars. https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#serialize-all-characters
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
TypeInfoResolverChain =
{
// Use Reflection based serialization/deserialization. Because Summary object contains anonymous type.
new DefaultJsonTypeInfoResolver(),
},
RespectNullableAnnotations = true,
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions