summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Serialisation/Converters/SemanticVersionConverter.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/Converters/SemanticVersionConverter.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/Converters/SemanticVersionConverter.cs')
-rw-r--r--src/StardewModdingAPI.Toolkit/Serialisation/Converters/SemanticVersionConverter.cs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Toolkit/Serialisation/Converters/SemanticVersionConverter.cs b/src/StardewModdingAPI.Toolkit/Serialisation/Converters/SemanticVersionConverter.cs
new file mode 100644
index 00000000..2075d27e
--- /dev/null
+++ b/src/StardewModdingAPI.Toolkit/Serialisation/Converters/SemanticVersionConverter.cs
@@ -0,0 +1,36 @@
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Toolkit.Serialisation.Models;
+
+namespace StardewModdingAPI.Toolkit.Serialisation.Converters
+{
+ /// <summary>Handles deserialisation of <see cref="SemanticVersion"/>.</summary>
+ internal class SemanticVersionConverter : SimpleReadOnlyConverter<SemanticVersion>
+ {
+ /*********
+ ** 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 SemanticVersion ReadObject(JObject obj, string path)
+ {
+ int major = obj.ValueIgnoreCase<int>("MajorVersion");
+ int minor = obj.ValueIgnoreCase<int>("MinorVersion");
+ int patch = obj.ValueIgnoreCase<int>("PatchVersion");
+ string build = obj.ValueIgnoreCase<string>("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 SemanticVersion 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 (SemanticVersion)version;
+ }
+ }
+}