summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Serialisation
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-10-14 11:44:02 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-10-14 11:44:02 -0400
commit79118316065a01322d8ea12a14589ec016794c32 (patch)
tree7a26668a66ea0630a2b9367ac820fe7a6d99ac77 /src/StardewModdingAPI/Framework/Serialisation
parentaf1a2bde8219c5d4b8660b13702725626a4a5647 (diff)
parent8aec1eff99858716afe7b8604b512181f78c214f (diff)
downloadSMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.gz
SMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.bz2
SMAPI-79118316065a01322d8ea12a14589ec016794c32.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI/Framework/Serialisation')
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs96
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs116
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs37
3 files changed, 0 insertions, 249 deletions
diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs
deleted file mode 100644
index 3193aa3c..00000000
--- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs
+++ /dev/null
@@ -1,96 +0,0 @@
-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);
- }
- }
-}
diff --git a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
deleted file mode 100644
index 11ffdccb..00000000
--- a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
+++ /dev/null
@@ -1,116 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using StardewModdingAPI.Framework.Exceptions;
-using StardewModdingAPI.Framework.Models;
-
-namespace StardewModdingAPI.Framework.Serialisation
-{
- /// <summary>Overrides how SMAPI reads and writes <see cref="ISemanticVersion"/> and <see cref="IManifestDependency"/> fields.</summary>
- internal class SFieldConverter : JsonConverter
- {
- /*********
- ** Accessors
- *********/
- /// <summary>Whether this converter can write JSON.</summary>
- public override bool CanWrite => false;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Get whether this instance can convert the specified object type.</summary>
- /// <param name="objectType">The object type.</param>
- public override bool CanConvert(Type objectType)
- {
- return
- objectType == typeof(ISemanticVersion)
- || objectType == typeof(IManifestDependency[])
- || objectType == typeof(ModCompatibilityID[]);
- }
-
- /// <summary>Reads the JSON representation of the object.</summary>
- /// <param name="reader">The JSON reader.</param>
- /// <param name="objectType">The object type.</param>
- /// <param name="existingValue">The object being read.</param>
- /// <param name="serializer">The calling serializer.</param>
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- // semantic version
- if (objectType == typeof(ISemanticVersion))
- {
- JToken token = JToken.Load(reader);
- switch (token.Type)
- {
- case JTokenType.Object:
- {
- JObject obj = (JObject)token;
- int major = obj.Value<int>(nameof(ISemanticVersion.MajorVersion));
- int minor = obj.Value<int>(nameof(ISemanticVersion.MinorVersion));
- int patch = obj.Value<int>(nameof(ISemanticVersion.PatchVersion));
- string build = obj.Value<string>(nameof(ISemanticVersion.Build));
- return new SemanticVersion(major, minor, patch, build);
- }
-
- case JTokenType.String:
- {
- string str = token.Value<string>();
- if (string.IsNullOrWhiteSpace(str))
- return null;
- if (!SemanticVersion.TryParse(str, out ISemanticVersion version))
- throw new SParseException($"Can't parse semantic version from invalid value '{str}', should be formatted like 1.2, 1.2.30, or 1.2.30-beta.");
- return version;
- }
-
- default:
- throw new SParseException($"Can't parse semantic version from {token.Type}, must be an object or string.");
- }
- }
-
- // manifest dependency
- if (objectType == typeof(IManifestDependency[]))
- {
- List<IManifestDependency> result = new List<IManifestDependency>();
- foreach (JObject obj in JArray.Load(reader).Children<JObject>())
- {
- string uniqueID = obj.Value<string>(nameof(IManifestDependency.UniqueID));
- string minVersion = obj.Value<string>(nameof(IManifestDependency.MinimumVersion));
-#if SMAPI_1_x
- result.Add(new ManifestDependency(uniqueID, minVersion));
-#else
- bool required = obj.Value<bool?>(nameof(IManifestDependency.IsRequired)) ?? true;
- result.Add(new ManifestDependency(uniqueID, minVersion, required));
-#endif
- }
- return result.ToArray();
- }
-
- // mod compatibility ID
- if (objectType == typeof(ModCompatibilityID[]))
- {
- List<ModCompatibilityID> result = new List<ModCompatibilityID>();
- foreach (JToken child in JArray.Load(reader).Children())
- {
- result.Add(child is JValue value
- ? new ModCompatibilityID(value.Value<string>())
- : child.ToObject<ModCompatibilityID>()
- );
- }
- return result.ToArray();
- }
-
- // unknown
- throw new NotSupportedException($"Unknown type '{objectType?.FullName}'.");
- }
-
- /// <summary>Writes the JSON representation of the object.</summary>
- /// <param name="writer">The JSON writer.</param>
- /// <param name="value">The value.</param>
- /// <param name="serializer">The calling serializer.</param>
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
- throw new InvalidOperationException("This converter does not write JSON.");
- }
- }
-}
diff --git a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
deleted file mode 100644
index 37108556..00000000
--- a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
+++ /dev/null
@@ -1,37 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Newtonsoft.Json.Converters;
-
-namespace StardewModdingAPI.Framework.Serialisation
-{
- /// <summary>A variant of <see cref="StringEnumConverter"/> which only converts certain enums.</summary>
- internal class SelectiveStringEnumConverter : StringEnumConverter
- {
- /*********
- ** Properties
- *********/
- /// <summary>The enum type names to convert.</summary>
- private readonly HashSet<string> Types;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="types">The enum types to convert.</param>
- public SelectiveStringEnumConverter(params Type[] types)
- {
- this.Types = new HashSet<string>(types.Select(p => p.FullName));
- }
-
- /// <summary>Get whether this instance can convert the specified object type.</summary>
- /// <param name="type">The object type.</param>
- public override bool CanConvert(Type type)
- {
- return
- base.CanConvert(type)
- && this.Types.Contains((Nullable.GetUnderlyingType(type) ?? type).FullName);
- }
- }
-}