summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Serialisation
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-08-11 20:33:21 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-08-11 20:33:21 -0400
commitb7907293349e95f84583e682f38e0eb491ac2e5d (patch)
tree3a2e2405729eb3a42b036049957ad741e9968f83 /src/StardewModdingAPI.Toolkit/Serialisation
parentef731de8318c7f01567baf2e23ae9a09789b4bdd (diff)
downloadSMAPI-b7907293349e95f84583e682f38e0eb491ac2e5d.tar.gz
SMAPI-b7907293349e95f84583e682f38e0eb491ac2e5d.tar.bz2
SMAPI-b7907293349e95f84583e682f38e0eb491ac2e5d.zip
add support for loading unpacked .json files through content API (#576)
Diffstat (limited to 'src/StardewModdingAPI.Toolkit/Serialisation')
-rw-r--r--src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs b/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
index 3cabbab3..cc8eeb73 100644
--- a/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
+++ b/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
@@ -32,10 +32,11 @@ namespace StardewModdingAPI.Toolkit.Serialisation
/// <summary>Read a JSON file.</summary>
/// <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
+ /// <param name="result">The parsed content model.</param>
+ /// <returns>Returns false if the file doesn't exist, else true.</returns>
+ /// <exception cref="ArgumentException">The given <paramref name="fullPath"/> is empty or invalid.</exception>
+ /// <exception cref="JsonReaderException">The file contains invalid JSON.</exception>
+ public bool ReadJsonFileIfExists<TModel>(string fullPath, out TModel result)
{
// validate
if (string.IsNullOrWhiteSpace(fullPath))
@@ -49,13 +50,15 @@ namespace StardewModdingAPI.Toolkit.Serialisation
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
- return null;
+ result = default(TModel);
+ return false;
}
// deserialise model
try
{
- return this.Deserialise<TModel>(json);
+ result = this.Deserialise<TModel>(json);
+ return true;
}
catch (Exception ex)
{