From 9d1b6a1af2f21dff9904d03daaf6f1ce6409d7f8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 14 Jan 2017 13:03:57 -0500 Subject: fix issue where default ICollection values in config.json were duplicated on each load (#209) --- src/StardewModdingAPI/Manifest.cs | 1 + src/StardewModdingAPI/ModHelper.cs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Manifest.cs b/src/StardewModdingAPI/Manifest.cs index 018b31ae..07dd3541 100644 --- a/src/StardewModdingAPI/Manifest.cs +++ b/src/StardewModdingAPI/Manifest.cs @@ -8,6 +8,7 @@ namespace StardewModdingAPI internal class ManifestImpl : Manifest, IManifest { /// The mod version. + [JsonProperty("Version", ObjectCreationHandling = ObjectCreationHandling.Auto/* avoids issue where Json.NET can't determine concrete type for interface */)] public new ISemanticVersion Version { get { return base.Version; } diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 7304c5ea..78b3eefa 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -10,6 +10,17 @@ namespace StardewModdingAPI [Obsolete("Use " + nameof(IModHelper) + " instead.")] // only direct mod access to this class is obsolete public class ModHelper : IModHelper { + /********* + ** Properties + *********/ + /// The JSON settings to use when serialising and deserialising files. + private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection values are duplicated each time the config is loaded + }; + + /********* ** Accessors *********/ @@ -82,7 +93,7 @@ namespace StardewModdingAPI } // deserialise model - TModel model = JsonConvert.DeserializeObject(json); + TModel model = JsonConvert.DeserializeObject(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); } } -- cgit