summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-06-05 20:22:46 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-06-05 20:22:46 -0400
commit625c538f244519700f3942b2b2969845db9a99b0 (patch)
treed13596220a62616838310cbcf0f0004e2f843da3 /src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
parent265ce35fd1db677230ddb16483b4d92e7c13a777 (diff)
downloadSMAPI-625c538f244519700f3942b2b2969845db9a99b0.tar.gz
SMAPI-625c538f244519700f3942b2b2969845db9a99b0.tar.bz2
SMAPI-625c538f244519700f3942b2b2969845db9a99b0.zip
move manifest parsing into toolkit (#532)
Diffstat (limited to 'src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs')
-rw-r--r--src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs b/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
new file mode 100644
index 00000000..00f334ad
--- /dev/null
+++ b/src/StardewModdingAPI.Toolkit/Serialisation/JsonHelper.cs
@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Newtonsoft.Json;
+using StardewModdingAPI.Toolkit.Serialisation.Converters;
+
+namespace StardewModdingAPI.Toolkit.Serialisation
+{
+ /// <summary>Encapsulates SMAPI's JSON file parsing.</summary>
+ public class JsonHelper
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The JSON settings to use when serialising and deserialising files.</summary>
+ public JsonSerializerSettings JsonSettings { get; } = new JsonSerializerSettings
+ {
+ Formatting = Formatting.Indented,
+ ObjectCreationHandling = ObjectCreationHandling.Replace, // avoid issue where default ICollection<T> values are duplicated each time the config is loaded
+ Converters = new List<JsonConverter> { new SemanticVersionConverter() }
+ };
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <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
+ {
+ // validate
+ if (string.IsNullOrWhiteSpace(fullPath))
+ throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath));
+
+ // read file
+ string json;
+ try
+ {
+ json = File.ReadAllText(fullPath);
+ }
+ catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
+ {
+ return null;
+ }
+
+ // deserialise model
+ try
+ {
+ return this.Deserialise<TModel>(json);
+ }
+ catch (Exception ex)
+ {
+ string error = $"Can't parse JSON file at {fullPath}.";
+
+ if (ex is JsonReaderException)
+ {
+ error += " This 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);
+ }
+ }
+
+ /// <summary>Save to a JSON file.</summary>
+ /// <typeparam name="TModel">The model type.</typeparam>
+ /// <param name="fullPath">The absolete file path.</param>
+ /// <param name="model">The model to save.</param>
+ /// <exception cref="InvalidOperationException">The given path is empty or invalid.</exception>
+ public void WriteJsonFile<TModel>(string fullPath, TModel model)
+ where TModel : class
+ {
+ // validate
+ if (string.IsNullOrWhiteSpace(fullPath))
+ throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath));
+
+ // create directory if needed
+ string dir = Path.GetDirectoryName(fullPath);
+ if (dir == null)
+ throw new ArgumentException("The file path is invalid.", nameof(fullPath));
+ if (!Directory.Exists(dir))
+ Directory.CreateDirectory(dir);
+
+ // write file
+ 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;
+ }
+ }
+ }
+}