Browse Source

Able to save as JSON

Piotr Czajkowski 3 years ago
parent
commit
602844f738
3 changed files with 27 additions and 1 deletions
  1. 10 0
      PSDText/PSDText.cs
  2. 5 1
      PSDText/PSDText.csproj
  3. 12 0
      PSDTextTests/PSDTextTests.cs

+ 10 - 0
PSDText/PSDText.cs

@@ -4,6 +4,7 @@ using System.IO;
 using System.Text;
 using System.Xml;
 using System.Xml.Serialization;
+using Newtonsoft.Json;
 
 namespace PSDText
 {
@@ -96,5 +97,14 @@ namespace PSDText
                 serializer.Serialize(sr, TextData);
             }
         }
+
+        public void SaveAsJSON(string path)
+        {
+            var serializer = new JsonSerializer();
+            using (var sr = new StreamWriter(path))
+            {
+                serializer.Serialize(sr, TextData);
+            }
+        }
     }
 }

+ 5 - 1
PSDText/PSDText.csproj

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

+ 12 - 0
PSDTextTests/PSDTextTests.cs

@@ -25,5 +25,17 @@ namespace PSDTextTests
             Assert.True(File.Exists(destination));
             File.Delete(destination);
         }
+
+        [Fact]
+        public void SaveAsJSON()
+        {
+            var test = new PSDText.PSDText(TestFile);
+            Assert.NotEmpty(test.TextData);
+
+            var destination = "./test.json";
+            test.SaveAsJSON(destination);
+            Assert.True(File.Exists(destination));
+            File.Delete(destination);
+        }
     }
 }