summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Serialisation
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/StardewModdingAPI/Framework/Serialisation
parent926894f8f52c2a5cf104fcac2f7f34b637f7b531 (diff)
downloadSMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.gz
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.bz2
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.zip
reorganise repo structure
Diffstat (limited to 'src/StardewModdingAPI/Framework/Serialisation')
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs96
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs121
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs37
3 files changed, 0 insertions, 254 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 917c950d..00000000
--- a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
+++ /dev/null
@@ -1,121 +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(ModDataID)
- || objectType == typeof(ModCompatibility[]);
- }
-
- /// <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 dependencies
- 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));
- bool required = obj.Value<bool?>(nameof(IManifestDependency.IsRequired)) ?? true;
- result.Add(new ManifestDependency(uniqueID, minVersion, required));
- }
- return result.ToArray();
- }
-
- // mod data ID
- if (objectType == typeof(ModDataID))
- {
- JToken token = JToken.Load(reader);
- return new ModDataID(token.Value<string>());
- }
-
- // mod compatibility records
- if (objectType == typeof(ModCompatibility[]))
- {
- List<ModCompatibility> result = new List<ModCompatibility>();
- foreach (JProperty property in JObject.Load(reader).Properties())
- {
- string range = property.Name;
- ModStatus status = (ModStatus)Enum.Parse(typeof(ModStatus), property.Value.Value<string>(nameof(ModCompatibility.Status)));
- string reasonPhrase = property.Value.Value<string>(nameof(ModCompatibility.ReasonPhrase));
-
- result.Add(new ModCompatibility(range, status, reasonPhrase));
- }
- 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);
- }
- }
-}