using Newtonsoft.Json.Linq;
using StardewModdingAPI.Toolkit.Serialisation.Models;
namespace StardewModdingAPI.Toolkit.Serialisation.Converters
{
/// Handles deserialisation of .
internal class SemanticVersionConverter : SimpleReadOnlyConverter
{
/*********
** Protected methods
*********/
/// Read a JSON object.
/// The JSON object to read.
/// The path to the current JSON node.
protected override SemanticVersion ReadObject(JObject obj, string path)
{
int major = obj.ValueIgnoreCase("MajorVersion");
int minor = obj.ValueIgnoreCase("MinorVersion");
int patch = obj.ValueIgnoreCase("PatchVersion");
string build = obj.ValueIgnoreCase("Build");
if (build == "0")
build = null; // '0' from incorrect examples in old SMAPI documentation
return new SemanticVersion(major, minor, patch, build);
}
/// Read a JSON string.
/// The JSON string value.
/// The path to the current JSON node.
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;
}
}
}