Skip to content

Commit

Permalink
Use json source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Aug 19, 2022
1 parent ac0beb3 commit b2e0e72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Bootstrap/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.CommandLine;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Microsoft.Build.Locator;
using TestMyCode.CSharp.Bootstrap;
using TestMyCode.CSharp.Core.Compiler;
using TestMyCode.CSharp.Core.Data;
using TestMyCode.CSharp.Core.Test;
Expand Down Expand Up @@ -84,7 +86,7 @@ static async Task<int> CommandHandler(bool generatePointsFile, bool runTests, Di
{
FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDir.FullName, ".tmc_available_points.json"));

await WriteToFile(resultsFile, projectData.Points);
await WriteToFile(resultsFile, projectData.Points, RunnerJsonContext.Default.IReadOnlyDictionaryStringHashSetString);
}

if (runTests)
Expand All @@ -94,10 +96,10 @@ static async Task<int> CommandHandler(bool generatePointsFile, bool runTests, Di

FileInfo resultsFile = outputFile ?? new FileInfo(Path.Combine(projectDir.FullName, ".tmc_test_results.json"));

await WriteToFile(resultsFile, testRunner.TestResults);
await WriteToFile(resultsFile, testRunner.TestResults, RunnerJsonContext.Default.IReadOnlyListMethodTestResult);
}

static async Task WriteToFile<T>(FileInfo resultsFile, T data)
static async Task WriteToFile<T>(FileInfo resultsFile, T data, JsonTypeInfo<T> jsonTypeInfo)
{
if (resultsFile.Exists)
{
Expand All @@ -106,10 +108,7 @@ static async Task WriteToFile<T>(FileInfo resultsFile, T data)

await using FileStream stream = resultsFile.Open(FileMode.OpenOrCreate, FileAccess.Write);

await JsonSerializer.SerializeAsync(stream, data, options: new JsonSerializerOptions
{
WriteIndented = true
});
await JsonSerializer.SerializeAsync(stream, data, jsonTypeInfo);
}

return 0;
Expand Down
11 changes: 11 additions & 0 deletions Bootstrap/RunnerJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
using TestMyCode.CSharp.Core.Test;

namespace TestMyCode.CSharp.Bootstrap;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(IReadOnlyDictionary<string, HashSet<string>>))]
[JsonSerializable(typeof(IReadOnlyList<MethodTestResult>))]
internal sealed partial class RunnerJsonContext : JsonSerializerContext
{
}

0 comments on commit b2e0e72

Please sign in to comment.