From c7c1e84f3ef401ad00f3f5a7f7957fc63fb19968 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 10 Aug 2023 11:13:54 +0200 Subject: [PATCH] Manually write JSON --- src/Exercism.TestRunner.CSharp/TestRun.cs | 18 ------ .../TestRunWriter.cs | 61 ++++++++++++++----- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/Exercism.TestRunner.CSharp/TestRun.cs b/src/Exercism.TestRunner.CSharp/TestRun.cs index 4ea1a65..009467c 100644 --- a/src/Exercism.TestRunner.CSharp/TestRun.cs +++ b/src/Exercism.TestRunner.CSharp/TestRun.cs @@ -11,37 +11,19 @@ internal enum TestStatus internal class TestResult { - [JsonPropertyName("name")] public string Name { get; set; } - - [JsonPropertyName("status")] public TestStatus Status { get; set; } - - [JsonPropertyName("task_id")] public int? TaskId { get; set; } - - [JsonPropertyName("message")] public string Message { get; set; } - - [JsonPropertyName("output")] public string Output { get; set; } - - [JsonPropertyName("test_code")] public string TestCode { get; set; } } internal class TestRun { - [JsonPropertyName("version")] public int Version { get; set; } = 3; - - [JsonPropertyName("status")] public TestStatus Status { get; set; } - - [JsonPropertyName("message")] public string Message { get; set; } - - [JsonPropertyName("tests")] public TestResult[] Tests { get; set; } } } \ No newline at end of file diff --git a/src/Exercism.TestRunner.CSharp/TestRunWriter.cs b/src/Exercism.TestRunner.CSharp/TestRunWriter.cs index f871901..6c61491 100644 --- a/src/Exercism.TestRunner.CSharp/TestRunWriter.cs +++ b/src/Exercism.TestRunner.CSharp/TestRunWriter.cs @@ -1,30 +1,59 @@ using System.IO; using System.Text.Json; -using System.Text.Json.Serialization; namespace Exercism.TestRunner.CSharp { internal static class TestRunWriter { - public static void WriteToFile(this TestRun testRun, Options options) => - File.WriteAllText(options.ResultsJsonPath(), testRun.ToJson()); + private static readonly JsonWriterOptions JsonWriterOptions = new() {Indented = true}; - private static string ResultsJsonPath(this Options options) => - Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json")); - - private static string ToJson(this TestRun testRun) => - JsonSerializer.Serialize(testRun, CreateJsonSerializerOptions()); - - private static JsonSerializerOptions CreateJsonSerializerOptions() + public static void WriteToFile(this TestRun testRun, Options options) + { + using var fileStream = File.Create(options.ResultsJsonPath()); + using var jsonWriter = new Utf8JsonWriter(fileStream, JsonWriterOptions); + jsonWriter.WriteStartObject(); + jsonWriter.WriteNumber("version", testRun.Version); + jsonWriter.WriteString("status", testRun.Status.ToString().ToLower()); + jsonWriter.WriteStringIfNotEmpty("message", testRun.Message); + jsonWriter.WriteTestResults(testRun.Tests); + jsonWriter.WriteEndObject(); + jsonWriter.Flush(); + fileStream.WriteByte((byte)'\n'); + } + + private static void WriteTestResults(this Utf8JsonWriter jsonWriter, TestResult[] testResults) { - var options = new JsonSerializerOptions + if (testResults.Length == 0) + return; + + jsonWriter.WriteStartArray("tests"); + + foreach (var testResult in testResults) { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - WriteIndented = true, - }; - options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); + jsonWriter.WriteStartObject(); + jsonWriter.WriteString("name", testResult.Name); + jsonWriter.WriteString("status", testResult.Status.ToString().ToLower()); + if (testResult.TaskId.HasValue) + jsonWriter.WriteNumber("task_id", testResult.TaskId.Value); + jsonWriter.WriteStringIfNotEmpty("message", testResult.Message); + jsonWriter.WriteStringIfNotEmpty("output", testResult.Output); + jsonWriter.WriteStringIfNotEmpty("test_code", testResult.TestCode); + jsonWriter.WriteEndObject(); + } + + jsonWriter.WriteEndArray(); + } - return options; + private static void WriteStringIfNotEmpty(this Utf8JsonWriter jsonWriter, string property, string value) + { + if (string.IsNullOrWhiteSpace(value)) + return; + + jsonWriter.WriteString(property, value); } + + + private static string ResultsJsonPath(this Options options) => + Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json")); } } \ No newline at end of file