using Newtonsoft.Json.Linq;
using StardewModdingAPI.Framework.Exceptions;
namespace StardewModdingAPI.Framework.Serialisation.SmapiConverters
{
/// 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 ISemanticVersion ReadObject(JObject obj, string path)
{
int major = obj.ValueIgnoreCase(nameof(ISemanticVersion.MajorVersion));
int minor = obj.ValueIgnoreCase(nameof(ISemanticVersion.MinorVersion));
int patch = obj.ValueIgnoreCase(nameof(ISemanticVersion.PatchVersion));
string build = obj.ValueIgnoreCase(nameof(ISemanticVersion.Build));
return new LegacyManifestVersion(major, minor, patch, build);
}
/// Read a JSON string.
/// The JSON string value.
/// The path to the current JSON node.
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;
}
}
}