Browse Source

Upgraded to .NET 6, removed NewtonSoft

Piotr Czajkowski 1 year ago
parent
commit
5dc9bf3d08
5 changed files with 51 additions and 52 deletions
  1. 26 0
      .github/workflows/dotnet.yml
  2. 0 19
      .github/workflows/dotnetcore.yml
  3. 18 24
      DOCX/DOCX.cs
  4. 1 5
      DOCX/DOCX.csproj
  5. 6 4
      DOCXTests/DOCXTests.csproj

+ 26 - 0
.github/workflows/dotnet.yml

@@ -0,0 +1,26 @@
+name: .NET
+
+on:
+  push:
+    branches: [ main ]
+  pull_request:
+    branches: [ main ]
+
+jobs:
+  build:
+
+    runs-on: ubuntu-latest
+
+    steps:
+    - uses: actions/checkout@v2
+    - name: Setup .NET
+      uses: actions/setup-dotnet@v1
+      with:
+        dotnet-version: 6.0.x
+    - name: Restore dependencies
+      run: dotnet restore
+    - name: Build
+      run: dotnet build --no-restore
+    - name: Test
+      run: dotnet test --no-build --verbosity normal
+

+ 0 - 19
.github/workflows/dotnetcore.yml

@@ -1,19 +0,0 @@
-name: .NET Core
-
-on: [push]
-
-jobs:
-  build:
-
-    runs-on: ubuntu-latest
-
-    steps:
-    - uses: actions/checkout@v2
-    - name: Setup .NET Core
-      uses: actions/setup-dotnet@v1
-      with:
-        dotnet-version: 3.1.101
-    - name: Test
-      run: dotnet test DOCXTests/
-    - name: Build with dotnet
-      run: dotnet build --configuration Release --framework netcoreapp3.1 DOCX/

+ 18 - 24
DOCX/DOCX.cs

@@ -3,8 +3,8 @@ using System.Collections.Generic;
 using System.IO;
 using System.IO.Compression;
 using System.Xml;
-using Newtonsoft.Json;
 using System.Linq;
+using System.Text.Json;
 
 namespace DOCX
 {
@@ -17,7 +17,7 @@ namespace DOCX
 
         private void LoadNamespace()
         {
-			_ns.AddNamespace("w", _wNamespace);
+            _ns.AddNamespace("w", _wNamespace);
         }
 
         public Docx(string path)
@@ -117,7 +117,7 @@ namespace DOCX
             _authors.Add(name, anonymousName);
             return anonymousName;
         }
-        
+
         private (bool status, string message) AnonymizeAuthors(ZipArchiveEntry comments)
         {
             var loadResult = GetXML(comments);
@@ -141,28 +141,23 @@ namespace DOCX
 
         private bool SaveAuthors(string path = null)
         {
-            if (string.IsNullOrEmpty(path)) {
+            if (string.IsNullOrEmpty(path))
+            {
                 if (!string.IsNullOrEmpty(_authorsJson))
                     path = _authorsJson;
                 else
                     return false;
             }
-            
-            using (StreamWriter sw = new StreamWriter(path))
-            using (JsonWriter writer = new JsonTextWriter(sw))
-            {
-                JsonSerializer serializer = new JsonSerializer
-                {
-                    NullValueHandling = NullValueHandling.Ignore
-                };
 
-                serializer.Serialize(writer, _authors);
+            using (var fs = File.OpenWrite(path))
+            {
+                JsonSerializer.Serialize(fs, _authors, typeof(Dictionary<string, string>));
             }
 
             return true;
         }
 
-        
+
         public (bool status, string message) AnonymizeComments(string path = null)
         {
             ZipArchiveEntry comments = _zip.GetEntry(@"word/comments.xml");
@@ -175,24 +170,23 @@ namespace DOCX
 
             return !SaveAuthors(path) ? (false, $"Problem saving authors to {path}!") : (true, "OK");
         }
-        
-        private bool LoadAuthors(string path=null)
+
+        private bool LoadAuthors(string path = null)
         {
             if (string.IsNullOrEmpty(path))
                 if (File.Exists(_authorsJson))
                     path = _authorsJson;
                 else
                     return false;
-            
-            using (StreamReader rd = new StreamReader(path))
+
+            using (var fs = File.OpenRead(path))
             {
-                string json = rd.ReadToEnd();
-                _authors = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
+                _authors = JsonSerializer.Deserialize<Dictionary<string, string>>(fs);
             }
 
             return _authors.Count > 0;
         }
-        
+
         private (bool status, string message) DeanonymizeAuthors(ZipArchiveEntry comments)
         {
             var loadResult = GetXML(comments);
@@ -214,14 +208,14 @@ namespace DOCX
 
             return SaveXML(doc, comments);
         }
-        
-        public (bool status, string message) DeanonymizeComments(string path=null)
+
+        public (bool status, string message) DeanonymizeComments(string path = null)
         {
             if (!LoadAuthors(path))
                 return (false, $"Can't load authors from {path}!");
 
             _authors = _authors.ToDictionary(x => x.Value, x => x.Key);
-            
+
             ZipArchiveEntry comments = _zip.GetEntry(@"word/comments.xml");
             if (comments == null)
                 return (false,

+ 1 - 5
DOCX/DOCX.csproj

@@ -1,12 +1,8 @@
 <Project Sdk="Microsoft.NET.Sdk">
 	<PropertyGroup>
-		<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
+		<TargetFramework>net6</TargetFramework>
 	</PropertyGroup>
 
-	<ItemGroup>
-		<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
-	</ItemGroup>
-
 	<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
 		<Reference Include="System.IO.Compression" />
 		<Reference Include="System.IO.Compression.FileSystem" />

+ 6 - 4
DOCXTests/DOCXTests.csproj

@@ -1,15 +1,17 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFramework>netcoreapp3.1</TargetFramework>
+        <TargetFramework>net6</TargetFramework>
 
         <IsPackable>false</IsPackable>
     </PropertyGroup>
 
     <ItemGroup>
-        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
-        <PackageReference Include="xunit" Version="2.2.0" />
-        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
+        <PackageReference Include="xunit" Version="2.4.1" />
+        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+<PrivateAssets>all</PrivateAssets>
+</PackageReference>
     </ItemGroup>
 
     <ItemGroup>