summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Serialisation/SmapiConverters
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/Serialisation/SmapiConverters')
-rw-r--r--src/SMAPI/Framework/Serialisation/SmapiConverters/ManifestDependencyArrayConverter.cs60
-rw-r--r--src/SMAPI/Framework/Serialisation/SmapiConverters/ModCompatibilityArrayConverter.cs61
-rw-r--r--src/SMAPI/Framework/Serialisation/SmapiConverters/ModDataIdConverter.cs19
-rw-r--r--src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs36
-rw-r--r--src/SMAPI/Framework/Serialisation/SmapiConverters/StringEnumConverter.cs22
5 files changed, 198 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Serialisation/SmapiConverters/ManifestDependencyArrayConverter.cs b/src/SMAPI/Framework/Serialisation/SmapiConverters/ManifestDependencyArrayConverter.cs
new file mode 100644
index 00000000..6352e367
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SmapiConverters/ManifestDependencyArrayConverter.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Framework.Models;
+
+namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
+{
+ /// <summary>Handles deserialisation of <see cref="IManifestDependency"/> arrays.</summary>
+ internal class ManifestDependencyArrayConverter : 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(IManifestDependency[]);
+ }
+
+
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read 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)
+ {
+ 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();
+ }
+
+ /// <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/SMAPI/Framework/Serialisation/SmapiConverters/ModCompatibilityArrayConverter.cs b/src/SMAPI/Framework/Serialisation/SmapiConverters/ModCompatibilityArrayConverter.cs
new file mode 100644
index 00000000..3232dde4
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SmapiConverters/ModCompatibilityArrayConverter.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Framework.Models;
+
+namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
+{
+ /// <summary>Handles deserialisation of <see cref="ModCompatibility"/> arrays.</summary>
+ internal class ModCompatibilityArrayConverter : 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(ModCompatibility[]);
+ }
+
+
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read 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)
+ {
+ 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();
+ }
+
+ /// <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/SMAPI/Framework/Serialisation/SmapiConverters/ModDataIdConverter.cs b/src/SMAPI/Framework/Serialisation/SmapiConverters/ModDataIdConverter.cs
new file mode 100644
index 00000000..8a10db47
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SmapiConverters/ModDataIdConverter.cs
@@ -0,0 +1,19 @@
+using StardewModdingAPI.Framework.Models;
+
+namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
+{
+ /// <summary>Handles deserialisation of <see cref="ModDataID"/>.</summary>
+ internal class ModDataIdConverter : SimpleReadOnlyConverter<ModDataID>
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read a JSON string.</summary>
+ /// <param name="str">The JSON string value.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override ModDataID ReadString(string str, string path)
+ {
+ return new ModDataID(str);
+ }
+ }
+}
diff --git a/src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs b/src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs
new file mode 100644
index 00000000..50181809
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs
@@ -0,0 +1,36 @@
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Framework.Exceptions;
+
+namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
+{
+ /// <summary>Handles deserialisation of <see cref="SemanticVersion"/>.</summary>
+ internal class SemanticVersionConverter : SimpleReadOnlyConverter<ISemanticVersion>
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read a JSON object.</summary>
+ /// <param name="obj">The JSON object to read.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override ISemanticVersion ReadObject(JObject obj, string path)
+ {
+ 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 LegacyManifestVersion(major, minor, patch, build);
+ }
+
+ /// <summary>Read a JSON string.</summary>
+ /// <param name="str">The JSON string value.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override ISemanticVersion ReadString(string str, string path)
+ {
+ 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 (path: {path}).");
+ return version;
+ }
+ }
+}
diff --git a/src/SMAPI/Framework/Serialisation/SmapiConverters/StringEnumConverter.cs b/src/SMAPI/Framework/Serialisation/SmapiConverters/StringEnumConverter.cs
new file mode 100644
index 00000000..c88ac834
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SmapiConverters/StringEnumConverter.cs
@@ -0,0 +1,22 @@
+using System;
+using Newtonsoft.Json.Converters;
+
+namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
+{
+ /// <summary>A variant of <see cref="StringEnumConverter"/> which only converts a specified enum.</summary>
+ /// <typeparam name="T">The enum type.</typeparam>
+ internal class StringEnumConverter<T> : StringEnumConverter
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <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)
+ && (Nullable.GetUnderlyingType(type) ?? type) == typeof(T);
+ }
+ }
+}