summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Serialisation/JsonHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-10-07 23:07:10 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-10-07 23:07:10 -0400
commit929dccb75a1405737975d76648e015a3e7c00177 (patch)
tree659fe16509327e694555db363caf7f47f326443b /src/SMAPI/Framework/Serialisation/JsonHelper.cs
parent926894f8f52c2a5cf104fcac2f7f34b637f7b531 (diff)
downloadSMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.gz
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.bz2
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.zip
reorganise repo structure
Diffstat (limited to 'src/SMAPI/Framework/Serialisation/JsonHelper.cs')
-rw-r--r--src/SMAPI/Framework/Serialisation/JsonHelper.cs96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Serialisation/JsonHelper.cs b/src/SMAPI/Framework/Serialisation/JsonHelper.cs
new file mode 100644
index 00000000..3193aa3c
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/JsonHelper.cs
@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Microsoft.Xna.Framework.Input;
+using Newtonsoft.Json;
+using StardewModdingAPI.Utilities;
+
+namespace StardewModdingAPI.Framework.Serialisation
+{
+ /// <summary>Encapsulates SMAPI's JSON file parsing.</summary>
+ internal class JsonHelper
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <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
+ Converters = new List<JsonConverter>
+ {
+ new SelectiveStringEnumConverter(typeof(Buttons), typeof(Keys), typeof(SButton))
+ }
+ };
+
+
+ /*********
+ ** 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 JsonConvert.DeserializeObject<TModel>(json, this.JsonSettings);
+ }
+ 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);
+ }
+ }
+
+ /// <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);
+ }
+ }
+}