Browse Source

Updated to .NET 6, removed NewtonSoft

Piotr Czajkowski 5 months ago
parent
commit
062264c94f

+ 0 - 9
PSDText/ISerializer.cs

@@ -1,9 +0,0 @@
-using System.IO;
-
-namespace PSDText
-{
-    public interface ISerializer
-    {
-        void Serialize(TextWriter textWriter, object o);
-    }
-}

+ 0 - 24
PSDText/MyJsonSerializer.cs

@@ -1,24 +0,0 @@
-using System;
-using System.IO;
-using Newtonsoft.Json;
-
-namespace PSDText
-{
-    public class MyJsonSerializer : ISerializer
-    {
-        private readonly JsonSerializer serializer;
-
-        public MyJsonSerializer(JsonSerializer jsonSerializer)
-        {
-            if (jsonSerializer == null)
-                throw new ArgumentNullException("jsonSerializer");
-
-            serializer = jsonSerializer;
-        }
-
-        public void Serialize(TextWriter textWriter, object o)
-        {
-            serializer.Serialize(textWriter, o);
-        }
-    }
-}

+ 0 - 24
PSDText/MyXmlSerializer.cs

@@ -1,24 +0,0 @@
-using System;
-using System.IO;
-using System.Xml.Serialization;
-
-namespace PSDText
-{
-    public class MyXmlSerializer : ISerializer
-    {
-        private readonly XmlSerializer serializer;
-
-        public MyXmlSerializer(XmlSerializer xmlSerializer)
-        {
-            if (xmlSerializer == null)
-                throw new ArgumentNullException("xmlSerializer");
-
-            serializer = xmlSerializer;
-        }
-
-        public void Serialize(TextWriter textWriter, object o)
-        {
-            serializer.Serialize(textWriter, o);
-        }
-    }
-}

+ 12 - 21
PSDText/PSDText.cs

@@ -3,19 +3,19 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
+using System.Text.Json;
 using System.Xml;
 using System.Xml.Serialization;
-using Newtonsoft.Json;
 
 namespace PSDText
 {
     public class PSDText
     {
         private readonly string _xmlData;
-        private readonly XmlNamespaceManager _ns = new XmlNamespaceManager(new NameTable());
-        public List<TextData> TextData;
+        private readonly XmlNamespaceManager _ns = new(new NameTable());
+        public List<TextData> ExtractedStrings;
 
-        private string Readxmpmeta(string path)
+        private static string Readxmpmeta(string path)
         {
             var sb = new StringBuilder(1000);
             using (var sr = new StreamReader(path))
@@ -94,19 +94,11 @@ namespace PSDText
                 throw new Exception("No data was read!");
 
             AddXMLNamespaces();
-            TextData = GetTextData();
-            if (!TextData.Any())
+            ExtractedStrings = GetTextData();
+            if (!ExtractedStrings.Any())
                 throw new Exception("Nothing was read from XML!");
         }
 
-        private void Serialize(string path, ISerializer serializer)
-        {
-            using (var sr = new StreamWriter(path))
-            {
-                serializer.Serialize(sr, TextData);
-            }
-        }
-
         /// <summary>
         /// Serializes text layers as XML.
         /// </summary>
@@ -114,11 +106,11 @@ namespace PSDText
         public void SaveAsXML(string path)
         {
             if (string.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException($"Wrong path: {path}");
+                throw new ArgumentNullException(nameof(path));
 
             var xmlSerializer = new XmlSerializer(typeof(List<TextData>));
-            var serializer = new MyXmlSerializer(xmlSerializer);
-            Serialize(path, serializer);
+            using var fs = File.OpenWrite(path);
+            xmlSerializer.Serialize(fs, ExtractedStrings);
         }
 
         /// <summary>
@@ -128,11 +120,10 @@ namespace PSDText
         public void SaveAsJSON(string path)
         {
             if (string.IsNullOrWhiteSpace(path))
-                throw new ArgumentNullException($"Wrong path: {path}");
+                throw new ArgumentNullException(nameof(path));
 
-            var jsonSerializer = new JsonSerializer();
-            var serializer = new MyJsonSerializer(jsonSerializer);
-            Serialize(path, serializer);
+            using var fs = File.OpenWrite(path);
+            JsonSerializer.Serialize(fs, ExtractedStrings, typeof(List<TextData>));
         }
     }
 }

+ 1 - 5
PSDText/PSDText.csproj

@@ -1,11 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net6.0</TargetFramework>
   </PropertyGroup>
 
-  <ItemGroup>
-    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
-  </ItemGroup>
-
 </Project>

+ 2 - 2
PSDText/TextData.cs

@@ -5,8 +5,8 @@ namespace PSDText
     public class TextData
     {
         [XmlAttribute]
-        public string Name;
+        public string Name { get; set; }
 
-        public string Text;
+        public string Text { get; set; }
     }
 }

+ 1 - 1
PSDTextTests/PSDTextTests.cs

@@ -13,7 +13,7 @@ namespace PSDTextTests
         public void ReadXMLFromPSD()
         {
             var test = new PSDText.PSDText(TestFile);
-            Assert.NotEmpty(test.TextData);
+            Assert.NotEmpty(test.ExtractedStrings);
         }
 
         [Fact]

+ 9 - 5
PSDTextTests/PSDTextTests.csproj

@@ -1,16 +1,20 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net6.0</TargetFramework>
 
     <IsPackable>false</IsPackable>
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
-    <PackageReference Include="xunit" Version="2.4.0" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
-    <PackageReference Include="coverlet.collector" Version="1.2.0" />
+    <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>
+    <PackageReference Include="coverlet.collector" Version="3.1.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+<PrivateAssets>all</PrivateAssets>
+</PackageReference>
   </ItemGroup>
 
   <ItemGroup>