From 7a4be5c18c23d294d28cfa114b998748f022b4a3 Mon Sep 17 00:00:00 2001 From: Daniel Palme Date: Thu, 7 Nov 2024 22:30:55 +0100 Subject: [PATCH] Removed custom Json serializer --- .../Common/JsonSerializerTest.cs | 49 ------- .../Common/JsonSerializer.cs | 128 ------------------ src/ReportGenerator.Core/Generator.cs | 6 +- .../Builders/CodeClimateReportBuilder.cs | 13 +- .../Builders/JsonSummaryReportBuilder.cs | 9 +- 5 files changed, 15 insertions(+), 190 deletions(-) delete mode 100644 src/ReportGenerator.Core.Test/Common/JsonSerializerTest.cs delete mode 100644 src/ReportGenerator.Core/Common/JsonSerializer.cs diff --git a/src/ReportGenerator.Core.Test/Common/JsonSerializerTest.cs b/src/ReportGenerator.Core.Test/Common/JsonSerializerTest.cs deleted file mode 100644 index 3b88ae44..00000000 --- a/src/ReportGenerator.Core.Test/Common/JsonSerializerTest.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Palmmedia.ReportGenerator.Core.Common; -using Xunit; - -namespace Palmmedia.ReportGenerator.Core.Test.Common -{ - public class JsonSerializerTest - { - [Fact] - public void ToJsonString_Null() - { - Assert.Equal("null", JsonSerializer.ToJsonString(null)); - } - - [Fact] - public void ToJsonString_PrimitiveType() - { - Assert.Equal("1", JsonSerializer.ToJsonString(1)); - Assert.Equal("2", JsonSerializer.ToJsonString(2d)); - Assert.Equal("2.45", JsonSerializer.ToJsonString(2.45d)); - Assert.Equal("3.41", JsonSerializer.ToJsonString(3.41m)); - Assert.Equal("4.5", JsonSerializer.ToJsonString(4.5f)); - Assert.Equal("true", JsonSerializer.ToJsonString(true)); - Assert.Equal("false", JsonSerializer.ToJsonString(false)); - } - - [Fact] - public void ToJsonString_String() - { - Assert.Equal("\"Text\"", JsonSerializer.ToJsonString("Text")); - } - - [Fact] - public void ToJsonString_Object() - { - var obj = new - { - Text = "Text", - Int = 1, - Double = 2.45d, - Decimal = 3.41, - Float = 4.5, - Boolean = true, - NestedObject = new { SubText = "Inner" } - }; - - Assert.Equal("{ \"Text\": \"Text\", \"Int\": 1, \"Double\": 2.45, \"Decimal\": 3.41, \"Float\": 4.5, \"Boolean\": true, \"NestedObject\": { \"SubText\": \"Inner\" } }", JsonSerializer.ToJsonString(obj)); - } - } -} diff --git a/src/ReportGenerator.Core/Common/JsonSerializer.cs b/src/ReportGenerator.Core/Common/JsonSerializer.cs deleted file mode 100644 index 62d16302..00000000 --- a/src/ReportGenerator.Core/Common/JsonSerializer.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; -using System.Globalization; -using System.Text; - -namespace Palmmedia.ReportGenerator.Core.Common -{ - /// - /// Simple JSON serializer. - /// - public class JsonSerializer - { - /// - /// Converts the given object to JSON. - /// - /// The object to convert. - /// The JSON string. - public static string ToJsonString(object obj) - { - if (obj == null) - { - return "null"; - } - - if (obj.GetType().IsValueType) - { - if (obj is bool) - { - return obj.ToString().ToLowerInvariant(); - } - else - { - return Convert.ToString(obj, CultureInfo.InvariantCulture); - } - } - else if (obj is string text) - { - return $"\"{EscapeString(text)}\""; - } - else - { - StringBuilder sb = new StringBuilder(); - sb.Append("{"); - - int counter = 0; - foreach (var property in obj.GetType().GetProperties()) - { - if (property.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length > 0) - { - continue; - } - - if (counter > 0) - { - sb.Append(","); - } - - sb.Append(" \""); - sb.Append(property.Name); - sb.Append("\": "); - sb.Append(ToJsonString(property.GetValue(obj))); - - counter++; - } - - sb.Append(" }"); - - return sb.ToString(); - } - } - - /// - /// Escapes string values. - /// - /// The input string. - /// The escaped string. - public static string EscapeString(string input) - { - if (input == null) - { - return null; - } - - bool NeedEscape(string src, int i) - { - char c = src[i]; - return c < 32 || c == '"' || c == '\\' - || (c >= '\uD800' && c <= '\uDBFF' && - (i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF')) - || (c >= '\uDC00' && c <= '\uDFFF' && - (i == 0 || src[i - 1] < '\uD800' || src[i - 1] > '\uDBFF')) - || c == '\u2028' || c == '\u2029' - || (c == '/' && i > 0 && src[i - 1] == '<'); - } - - var sb = new StringBuilder(); - - int start = 0; - for (int i = 0; i < input.Length; i++) - { - if (NeedEscape(input, i)) - { - sb.Append(input, start, i - start); - switch (input[i]) - { - case '\b': sb.Append("\\b"); break; - case '\f': sb.Append("\\f"); break; - case '\n': sb.Append("\\n"); break; - case '\r': sb.Append("\\r"); break; - case '\t': sb.Append("\\t"); break; - case '\"': sb.Append("\\\""); break; - case '\\': sb.Append("\\\\"); break; - case '/': sb.Append("\\/"); break; - default: - sb.Append("\\u"); - sb.Append(((int)input[i]).ToString("x04")); - break; - } - - start = i + 1; - } - } - - sb.Append(input, start, input.Length - start); - - return sb.ToString(); - } - } -} diff --git a/src/ReportGenerator.Core/Generator.cs b/src/ReportGenerator.Core/Generator.cs index 61eb56e4..ff719504 100644 --- a/src/ReportGenerator.Core/Generator.cs +++ b/src/ReportGenerator.Core/Generator.cs @@ -152,9 +152,9 @@ public bool GenerateReport( } Logger.Debug(Resources.Settings); - Logger.Debug(" " + JsonSerializer.ToJsonString(settings)); - Logger.Debug(" " + JsonSerializer.ToJsonString(minimumCoverageThresholds)); - Logger.Debug(" " + JsonSerializer.ToJsonString(riskHotspotsAnalysisThresholds)); + Logger.Debug(" " + System.Text.Json.JsonSerializer.Serialize(settings)); + Logger.Debug(" " + System.Text.Json.JsonSerializer.Serialize(minimumCoverageThresholds)); + Logger.Debug(" " + System.Text.Json.JsonSerializer.Serialize(riskHotspotsAnalysisThresholds)); var stopWatch = Stopwatch.StartNew(); diff --git a/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs index 43f4baaa..a270d170 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/CodeClimateReportBuilder.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.IO; using System.Text; +using System.Text.Json; using Palmmedia.ReportGenerator.Core.Common; using Palmmedia.ReportGenerator.Core.Logging; using Palmmedia.ReportGenerator.Core.Parser.Analysis; @@ -84,7 +85,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine("{"); reportTextWriter.WriteLine(" \"ci_service\": {"); - reportTextWriter.WriteLine($" \"branch\": \"{JsonSerializer.EscapeString(gitInformation.Branch)}\","); + reportTextWriter.WriteLine($" \"branch\": {JsonSerializer.Serialize(gitInformation.Branch)},"); reportTextWriter.WriteLine(" \"build_identifier\": \"\","); reportTextWriter.WriteLine(" \"build_url\": \"\","); reportTextWriter.WriteLine($" \"commit_sha\": \"{gitInformation.Sha}\","); @@ -95,15 +96,15 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(" \"environment\": {"); reportTextWriter.WriteLine(" \"gem_version\": \"\","); reportTextWriter.WriteLine(" \"package_version\": \"\","); - reportTextWriter.WriteLine($" \"pwd\": \"{JsonSerializer.EscapeString(Directory.GetCurrentDirectory())}\","); - reportTextWriter.WriteLine($" \"prefix\": \"{JsonSerializer.EscapeString(Directory.GetCurrentDirectory())}\","); + reportTextWriter.WriteLine($" \"pwd\": {JsonSerializer.Serialize(Directory.GetCurrentDirectory())},"); + reportTextWriter.WriteLine($" \"prefix\": {JsonSerializer.Serialize(Directory.GetCurrentDirectory())},"); reportTextWriter.WriteLine(" \"rails_root\": \"\","); reportTextWriter.WriteLine(" \"reporter_version\": \"0.11.1\","); reportTextWriter.WriteLine(" \"simplecov_root\": \"\""); reportTextWriter.WriteLine(" },"); reportTextWriter.WriteLine(" \"git\": {"); - reportTextWriter.WriteLine($" \"branch\": \"{JsonSerializer.EscapeString(gitInformation.Branch)}\","); + reportTextWriter.WriteLine($" \"branch\": {JsonSerializer.Serialize(gitInformation.Branch)},"); reportTextWriter.WriteLine($" \"head\": \"{gitInformation.Sha}\","); reportTextWriter.WriteLine($" \"committed_at\": {(string.IsNullOrWhiteSpace(gitInformation.TimeStamp) ? "null" : gitInformation.TimeStamp)}"); reportTextWriter.WriteLine(" },"); @@ -140,7 +141,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) } reportTextWriter.WriteLine(" {"); - reportTextWriter.WriteLine($" \"blob_id\": \"{JsonSerializer.EscapeString(GitHelper.GetFileHash(file.Path))}\","); + reportTextWriter.WriteLine($" \"blob_id\": {JsonSerializer.Serialize(GitHelper.GetFileHash(file.Path))},"); reportTextWriter.Write($" \"coverage\": ["); var lineCoverage = file.LineCoverage; @@ -170,7 +171,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine($" \"covered\": {file.CoverableLines.ToString(CultureInfo.InvariantCulture)},"); reportTextWriter.WriteLine($" \"total\": {file.TotalLines.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)}"); reportTextWriter.WriteLine(" },"); - reportTextWriter.WriteLine($" \"name\": \"{JsonSerializer.EscapeString(file.Path)}\""); + reportTextWriter.WriteLine($" \"name\": {JsonSerializer.Serialize(file.Path)}"); reportTextWriter.Write(" }"); } } diff --git a/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs b/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs index d18aa3d9..8ffed037 100644 --- a/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs +++ b/src/ReportGenerator.Core/Reporting/Builders/JsonSummaryReportBuilder.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Text.Json; using Palmmedia.ReportGenerator.Core.Common; using Palmmedia.ReportGenerator.Core.Logging; using Palmmedia.ReportGenerator.Core.Parser.Analysis; @@ -141,11 +142,11 @@ public void CreateSummaryReport(SummaryResult summaryResult) if (metric.Value.HasValue) { - reportTextWriter.Write($" {{ \"name\": \"{JsonSerializer.EscapeString(metric.Name)}\", \"value\": {metric.Value.Value.ToString(CultureInfo.InvariantCulture)} }}"); + reportTextWriter.Write($" {{ \"name\": {JsonSerializer.Serialize(metric.Name)}, \"value\": {metric.Value.Value.ToString(CultureInfo.InvariantCulture)} }}"); } else { - reportTextWriter.Write($" {{ \"name\": \"{JsonSerializer.EscapeString(metric.Name)}\", \"value\": null }}"); + reportTextWriter.Write($" {{ \"name\": {JsonSerializer.Serialize(metric.Name)}, \"value\": null }}"); } metricCounter++; @@ -167,7 +168,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(","); } - reportTextWriter.WriteLine($" {{ \"name\": \"{JsonSerializer.EscapeString(assembly.Name)}\", \"classes\": {assembly.Classes.Count().ToString(CultureInfo.InvariantCulture)}, \"coverage\": {(assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredlines\": {assembly.CoveredLines.ToString(CultureInfo.InvariantCulture)}, \"coverablelines\": {assembly.CoverableLines.ToString(CultureInfo.InvariantCulture)}, \"totallines\": {(assembly.TotalLines.HasValue ? assembly.TotalLines.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"branchcoverage\": {(assembly.BranchCoverageQuota.HasValue ? assembly.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredbranches\": {(assembly.CoveredBranches.HasValue ? assembly.CoveredBranches.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"totalbranches\": {(assembly.TotalBranches.HasValue ? assembly.TotalBranches.Value.ToString(CultureInfo.InvariantCulture) : null)}, \"methodcoverage\": {(assembly.CodeElementCoverageQuota.HasValue ? assembly.CodeElementCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredmethods\": {assembly.CoveredCodeElements.ToString(CultureInfo.InvariantCulture)}, \"totalmethods\": {assembly.TotalCodeElements.ToString(CultureInfo.InvariantCulture)}, \"classesinassembly\": ["); + reportTextWriter.WriteLine($" {{ \"name\": {JsonSerializer.Serialize(assembly.Name)}, \"classes\": {assembly.Classes.Count().ToString(CultureInfo.InvariantCulture)}, \"coverage\": {(assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredlines\": {assembly.CoveredLines.ToString(CultureInfo.InvariantCulture)}, \"coverablelines\": {assembly.CoverableLines.ToString(CultureInfo.InvariantCulture)}, \"totallines\": {(assembly.TotalLines.HasValue ? assembly.TotalLines.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"branchcoverage\": {(assembly.BranchCoverageQuota.HasValue ? assembly.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredbranches\": {(assembly.CoveredBranches.HasValue ? assembly.CoveredBranches.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"totalbranches\": {(assembly.TotalBranches.HasValue ? assembly.TotalBranches.Value.ToString(CultureInfo.InvariantCulture) : null)}, \"methodcoverage\": {(assembly.CodeElementCoverageQuota.HasValue ? assembly.CodeElementCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredmethods\": {assembly.CoveredCodeElements.ToString(CultureInfo.InvariantCulture)}, \"totalmethods\": {assembly.TotalCodeElements.ToString(CultureInfo.InvariantCulture)}, \"classesinassembly\": ["); int classCounter = 0; @@ -178,7 +179,7 @@ public void CreateSummaryReport(SummaryResult summaryResult) reportTextWriter.WriteLine(","); } - reportTextWriter.Write($" {{ \"name\": \"{JsonSerializer.EscapeString(@class.Name)}\", \"coverage\": {(@class.CoverageQuota.HasValue ? @class.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredlines\": {@class.CoveredLines.ToString(CultureInfo.InvariantCulture)}, \"coverablelines\": {@class.CoverableLines.ToString(CultureInfo.InvariantCulture)}, \"totallines\": {(@class.TotalLines.HasValue ? @class.TotalLines.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"branchcoverage\": {(@class.BranchCoverageQuota.HasValue ? @class.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredbranches\": {(@class.CoveredBranches.HasValue ? @class.CoveredBranches.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"totalbranches\": {(@class.TotalBranches.HasValue ? @class.TotalBranches.Value.ToString(CultureInfo.InvariantCulture) : null)}, \"methodcoverage\": {(@class.CodeElementCoverageQuota.HasValue ? @class.CodeElementCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredmethods\": {@class.CoveredCodeElements.ToString(CultureInfo.InvariantCulture)}, \"totalmethods\": {@class.TotalCodeElements.ToString(CultureInfo.InvariantCulture)} }}"); + reportTextWriter.Write($" {{ \"name\": {JsonSerializer.Serialize(@class.Name)}, \"coverage\": {(@class.CoverageQuota.HasValue ? @class.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredlines\": {@class.CoveredLines.ToString(CultureInfo.InvariantCulture)}, \"coverablelines\": {@class.CoverableLines.ToString(CultureInfo.InvariantCulture)}, \"totallines\": {(@class.TotalLines.HasValue ? @class.TotalLines.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"branchcoverage\": {(@class.BranchCoverageQuota.HasValue ? @class.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredbranches\": {(@class.CoveredBranches.HasValue ? @class.CoveredBranches.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"totalbranches\": {(@class.TotalBranches.HasValue ? @class.TotalBranches.Value.ToString(CultureInfo.InvariantCulture) : null)}, \"methodcoverage\": {(@class.CodeElementCoverageQuota.HasValue ? @class.CodeElementCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) : "null")}, \"coveredmethods\": {@class.CoveredCodeElements.ToString(CultureInfo.InvariantCulture)}, \"totalmethods\": {@class.TotalCodeElements.ToString(CultureInfo.InvariantCulture)} }}"); classCounter++; }