From dc2ceb39f31c35752c943b5052d5abaa7b6494fa Mon Sep 17 00:00:00 2001
From: Jesse Plamondon-Willard <github@jplamondonw.com>
Date: Fri, 12 Jan 2018 01:11:16 -0500
Subject: fix curly quotes in config.json automatically if possible (#412)

---
 src/SMAPI/Framework/Serialisation/JsonHelper.cs | 44 ++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 9 deletions(-)

(limited to 'src/SMAPI/Framework/Serialisation')

diff --git a/src/SMAPI/Framework/Serialisation/JsonHelper.cs b/src/SMAPI/Framework/Serialisation/JsonHelper.cs
index d923ec0c..7c4e3ee3 100644
--- a/src/SMAPI/Framework/Serialisation/JsonHelper.cs
+++ b/src/SMAPI/Framework/Serialisation/JsonHelper.cs
@@ -55,18 +55,15 @@ namespace StardewModdingAPI.Framework.Serialisation
             // deserialise model
             try
             {
-                return JsonConvert.DeserializeObject<TModel>(json, this.JsonSettings);
+                return this.Deserialise<TModel>(json);
             }
             catch (JsonReaderException ex)
             {
-                string message = $"The file at {fullPath} doesn't seem to be valid JSON.";
-
-                string text = File.ReadAllText(fullPath);
-                if (text.Contains("“") || text.Contains("”"))
-                    message += " Found curly quotes in the text; note that only straight quotes are allowed in JSON.";
-
-                message += $"\nTechnical details: {ex.Message}";
-                throw new JsonReaderException(message);
+                string error = $"The file at {fullPath} doesn't seem to be valid JSON.";
+                if (json.Contains("“") || json.Contains("”"))
+                    error += " Found curly quotes in the text; note that only straight quotes are allowed in JSON.";
+                error += $"\nTechnical details: {ex.Message}";
+                throw new JsonReaderException(error);
             }
         }
 
@@ -93,5 +90,34 @@ namespace StardewModdingAPI.Framework.Serialisation
             string json = JsonConvert.SerializeObject(model, this.JsonSettings);
             File.WriteAllText(fullPath, json);
         }
+
+
+        /*********
+        ** Private methods
+        *********/
+        /// <summary>Deserialize JSON text if possible.</summary>
+        /// <typeparam name="TModel">The model type.</typeparam>
+        /// <param name="json">The raw JSON text.</param>
+        private TModel Deserialise<TModel>(string json)
+        {
+            try
+            {
+                return JsonConvert.DeserializeObject<TModel>(json, this.JsonSettings);
+            }
+            catch (JsonReaderException)
+            {
+                // try replacing curly quotes
+                if (json.Contains("“") || json.Contains("”"))
+                {
+                    try
+                    {
+                        return JsonConvert.DeserializeObject<TModel>(json.Replace('“', '"').Replace('”', '"'), this.JsonSettings);
+                    }
+                    catch { /* rethrow original error */ }
+                }
+
+                throw;
+            }
+        }
     }
 }
-- 
cgit