Browse Source

SaveAsJSON returns bool

Piotr Czajkowski 3 years ago
parent
commit
d55b3ad83b
2 changed files with 15 additions and 5 deletions
  1. 12 1
      PSDText/PSDText.cs
  2. 3 4
      PSDTextTests/PSDTextTests.cs

+ 12 - 1
PSDText/PSDText.cs

@@ -107,13 +107,24 @@ namespace PSDText
             return true;
         }
 
-        public void SaveAsJSON(string path)
+        /// <summary>
+        /// Serializes text layers as JSON.
+        /// </summary>
+        /// <param name="path">Output JSON path.</param>
+        /// <returns>True on successful serialization,
+        /// false if there's nothing to write.</returns>
+        public bool SaveAsJSON(string path)
         {
+            if (!TextData.Any())
+                return false;
+
             var serializer = new JsonSerializer();
             using (var sr = new StreamWriter(path))
             {
                 serializer.Serialize(sr, TextData);
             }
+
+            return true;
         }
     }
 }

+ 3 - 4
PSDTextTests/PSDTextTests.cs

@@ -45,7 +45,7 @@ namespace PSDTextTests
             Assert.NotEmpty(test.TextData);
 
             var destination = "./test.json";
-            test.SaveAsJSON(destination);
+            Assert.True(test.SaveAsJSON(destination));
             Assert.True(File.Exists(destination));
             File.Delete(destination);
         }
@@ -57,9 +57,8 @@ namespace PSDTextTests
             Assert.Empty(test.TextData);
 
             var destination = "./test.json";
-            test.SaveAsJSON(destination);
-            Assert.True(File.Exists(destination));
-            File.Delete(destination);
+            Assert.False(test.SaveAsJSON(destination));
+            Assert.False(File.Exists(destination));
         }
     }
 }