diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-20 21:45:21 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-01-20 21:45:21 -0500 |
commit | 454a2de2949deae90de4a936fb8fa7341d4a2990 (patch) | |
tree | c8e1230162425a5bf613345a70dc79856630d93d /src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs | |
parent | 894fd25a18cd5ddf31a905860aca95c438894efd (diff) | |
download | SMAPI-454a2de2949deae90de4a936fb8fa7341d4a2990.tar.gz SMAPI-454a2de2949deae90de4a936fb8fa7341d4a2990.tar.bz2 SMAPI-454a2de2949deae90de4a936fb8fa7341d4a2990.zip |
split SFieldConverter into separate converters
Diffstat (limited to 'src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs')
-rw-r--r-- | src/SMAPI/Framework/Serialisation/SmapiConverters/SemanticVersionConverter.cs | 36 |
1 files changed, 36 insertions, 0 deletions
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; + } + } +} |