Skip to content

Commit f1f5908

Browse files
committed
added json format option
1 parent 6f4c4c4 commit f1f5908

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/LineCountReportJsonContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
using LineCount;
3+
4+
[JsonSerializable(typeof(LineCountReport))]
5+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization, PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, WriteIndented = true)]
6+
public partial class LineCountReportJsonContext : JsonSerializerContext
7+
{
8+
9+
}

src/Logging/Logger.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using System.Text.Json;
23
using LineCount.Errors;
34

45
namespace LineCount.Logging;
@@ -51,6 +52,9 @@ public static void LogReport(LineCountReport report, Format format)
5152
case Format.Raw:
5253
LogRawReport(report);
5354
break;
55+
case Format.Json:
56+
LogJsonReport(report);
57+
break;
5458
}
5559
}
5660

@@ -69,4 +73,10 @@ static void LogRawReport(LineCountReport report)
6973
{
7074
Console.WriteLine(report.Lines);
7175
}
76+
77+
static void LogJsonReport(LineCountReport report)
78+
{
79+
string json = JsonSerializer.Serialize(report, LineCountReportJsonContext.Default.LineCountReport);
80+
Console.WriteLine(json);
81+
}
7282
}

0 commit comments

Comments
 (0)