summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/ModHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-01-16 16:10:57 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-01-16 16:10:57 -0500
commit6adf199987a506f8a65f6c1ddfad5aa9fa2a6a9f (patch)
treefaa9155fae99533110853567003325486f208937 /src/StardewModdingAPI/ModHelper.cs
parente8825947ca82c8f28ad9bc8a225fb4fb749814cb (diff)
parent1f3d3c8c93c7a427486b60cf649b86cef140e88b (diff)
downloadSMAPI-6adf199987a506f8a65f6c1ddfad5aa9fa2a6a9f.tar.gz
SMAPI-6adf199987a506f8a65f6c1ddfad5aa9fa2a6a9f.tar.bz2
SMAPI-6adf199987a506f8a65f6c1ddfad5aa9fa2a6a9f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI/ModHelper.cs')
-rw-r--r--src/StardewModdingAPI/ModHelper.cs17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs
index 1fcc0182..78b3eefa 100644
--- a/src/StardewModdingAPI/ModHelper.cs
+++ b/src/StardewModdingAPI/ModHelper.cs
@@ -11,6 +11,17 @@ namespace StardewModdingAPI
public class ModHelper : IModHelper
{
/*********
+ ** Properties
+ *********/
+ /// <summary>The JSON settings to use when serialising and deserialising files.</summary>
+ private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
+ {
+ Formatting = Formatting.Indented,
+ ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection<T> values are duplicated each time the config is loaded
+ };
+
+
+ /*********
** Accessors
*********/
/// <summary>The mod directory path.</summary>
@@ -76,13 +87,13 @@ namespace StardewModdingAPI
{
json = File.ReadAllText(fullPath);
}
- catch (FileNotFoundException)
+ catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
return null;
}
// deserialise model
- TModel model = JsonConvert.DeserializeObject<TModel>(json);
+ TModel model = JsonConvert.DeserializeObject<TModel>(json, this.JsonSettings);
if (model is IConfigFile)
{
var wrapper = (IConfigFile)model;
@@ -108,7 +119,7 @@ namespace StardewModdingAPI
Directory.CreateDirectory(dir);
// write file
- string json = JsonConvert.SerializeObject(model, Formatting.Indented);
+ string json = JsonConvert.SerializeObject(model, this.JsonSettings);
File.WriteAllText(path, json);
}
}