Skip to content

Commit 0b559ca

Browse files
committed
Switch to System.Text.Json to get rid of Newtonsoft.Json dependency
1 parent e7e45b0 commit 0b559ca

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/CheckTestOutput.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.0;net5.0;net6.0</TargetFrameworks>
55

66
<Description>Simple helper which checks that output of a test matches a file. If not matching, just git staging the new file will accept the new version.</Description>
77
<PackageId>CheckTestOutput</PackageId>
@@ -19,9 +19,12 @@
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
2020
</PropertyGroup>
2121

22+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
23+
<PackageReference Include="System.Text.Json" Version="5.0.2" />
24+
</ItemGroup>
25+
2226
<ItemGroup>
23-
<PackageReference Include="MedallionShell" Version="1.5.1" />
24-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
27+
<PackageReference Include="MedallionShell" Version="1.6.2" />
2528

2629
<None Include="../LICENSE" Pack="true" PackagePath=""/>
2730
<None Include="../README.md" Pack="true" PackagePath=""/>

src/JsonChecks.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using Newtonsoft.Json;
5+
using System.Text.Json;
6+
using System.Text.RegularExpressions;
67

78
namespace CheckTestOutput
89
{
@@ -16,17 +17,14 @@ public static void CheckJsonObject(
1617
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
1718
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null)
1819
{
19-
var serializer = new JsonSerializer();
20-
var outputString = new System.Text.StringBuilder();
21-
using (var w = new JsonTextWriter(new StringWriter(outputString)))
22-
{
23-
w.Indentation = 1;
24-
w.IndentChar = '\t';
25-
w.Formatting = Formatting.Indented;
26-
serializer.Serialize(w, output);
27-
}
20+
var strOutput =
21+
JsonSerializer.Serialize(output, new JsonSerializerOptions() { WriteIndented = true });
22+
23+
24+
// indent using tabs for back compatibility
25+
strOutput = Regex.Replace(strOutput, "^( )+", m => new string('\t', m.Value.Length / 2), RegexOptions.Multiline);
2826
t.CheckOutputCore(
29-
outputString.ToString(),
27+
strOutput,
3028
checkName,
3129
$"{Path.GetFileNameWithoutExtension(sourceFilePath)}.{memberName}",
3230
fileExtension

0 commit comments

Comments
 (0)