From e7606884adee2ada103439aca2d53cc29b57da17 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 14:40:55 -0400 Subject: handle edge case in JSON file read/write code --- src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs') diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs index bd15c7bb..64d8738e 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -31,9 +31,14 @@ namespace StardewModdingAPI.Framework.Serialisation /// The model type. /// The absolete file path. /// Returns the deserialised model, or null if the file doesn't exist or is empty. + /// The given path is empty or invalid. public TModel ReadJsonFile(string fullPath) where TModel : class { + // validate + if (string.IsNullOrWhiteSpace(fullPath)) + throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath)); + // read file string json; try @@ -53,11 +58,18 @@ namespace StardewModdingAPI.Framework.Serialisation /// The model type. /// The absolete file path. /// The model to save. + /// The given path is empty or invalid. public void WriteJsonFile(string fullPath, TModel model) where TModel : class { + // validate + if (string.IsNullOrWhiteSpace(fullPath)) + throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath)); + // create directory if needed string dir = Path.GetDirectoryName(fullPath); + if (dir == null) + throw new ArgumentException("The file path is invalid.", nameof(fullPath)); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); -- cgit