diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-12 01:11:16 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-12 01:11:16 -0500 |
commit | dc2ceb39f31c35752c943b5052d5abaa7b6494fa (patch) | |
tree | 6c621389d64be9085098c74610a55b83685b4c39 /src/SMAPI/Framework/Serialisation | |
parent | d8a3ed36c849fdeb1a2d9ca9f55682dc8e5d21fb (diff) | |
download | SMAPI-dc2ceb39f31c35752c943b5052d5abaa7b6494fa.tar.gz SMAPI-dc2ceb39f31c35752c943b5052d5abaa7b6494fa.tar.bz2 SMAPI-dc2ceb39f31c35752c943b5052d5abaa7b6494fa.zip |
fix curly quotes in config.json automatically if possible (#412)
Diffstat (limited to 'src/SMAPI/Framework/Serialisation')
-rw-r--r-- | src/SMAPI/Framework/Serialisation/JsonHelper.cs | 44 |
1 files changed, 35 insertions, 9 deletions
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; + } + } } } |