diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-04-26 14:40:55 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-04-26 14:40:55 -0400 |
commit | e7606884adee2ada103439aca2d53cc29b57da17 (patch) | |
tree | 671b3a5061e5f49a9879014462058b07733d8749 | |
parent | 89221b8b2d88668238d89af38484784c8bd16116 (diff) | |
download | SMAPI-e7606884adee2ada103439aca2d53cc29b57da17.tar.gz SMAPI-e7606884adee2ada103439aca2d53cc29b57da17.tar.bz2 SMAPI-e7606884adee2ada103439aca2d53cc29b57da17.zip |
handle edge case in JSON file read/write code
-rw-r--r-- | src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs | 12 |
1 files changed, 12 insertions, 0 deletions
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 /// <typeparam name="TModel">The model type.</typeparam> /// <param name="fullPath">The absolete file path.</param> /// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns> + /// <exception cref="InvalidOperationException">The given path is empty or invalid.</exception> public TModel ReadJsonFile<TModel>(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 /// <typeparam name="TModel">The model type.</typeparam> /// <param name="fullPath">The absolete file path.</param> /// <param name="model">The model to save.</param> + /// <exception cref="InvalidOperationException">The given path is empty or invalid.</exception> public void WriteJsonFile<TModel>(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); |