From eaab81db0b2c96e5a4a92cc9e64693d4256d0141 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Wed, 2 Jun 2021 04:23:37 -0500 Subject: [PATCH] Fix SemVer converters to throw when the value is not valid --- IPA.Loader/JsonConverters/SemverRangeConverter.cs | 8 ++++++-- IPA.Loader/JsonConverters/SemverVersionConverter.cs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/IPA.Loader/JsonConverters/SemverRangeConverter.cs b/IPA.Loader/JsonConverters/SemverRangeConverter.cs index edff82b8..71e84983 100644 --- a/IPA.Loader/JsonConverters/SemverRangeConverter.cs +++ b/IPA.Loader/JsonConverters/SemverRangeConverter.cs @@ -1,5 +1,6 @@ #nullable enable using System; +using System.Runtime.Remoting.Messaging; using Hive.Versioning; using Newtonsoft.Json; @@ -8,9 +9,12 @@ namespace IPA.JsonConverters internal class SemverRangeConverter : JsonConverter { public override VersionRange? ReadJson(JsonReader reader, Type objectType, VersionRange? existingValue, bool hasExistingValue, JsonSerializer serializer) - => reader.Value is string s && VersionRange.TryParse(s, out var range) ? range : existingValue; + => reader.Value is not string s ? existingValue : new VersionRange(s); public override void WriteJson(JsonWriter writer, VersionRange? value, JsonSerializer serializer) - => writer.WriteValue(value?.ToString()); + { + if (value is null) writer.WriteNull(); + else writer.WriteValue(value.ToString()); + } } } diff --git a/IPA.Loader/JsonConverters/SemverVersionConverter.cs b/IPA.Loader/JsonConverters/SemverVersionConverter.cs index c0c7c10a..40683f03 100644 --- a/IPA.Loader/JsonConverters/SemverVersionConverter.cs +++ b/IPA.Loader/JsonConverters/SemverVersionConverter.cs @@ -8,7 +8,7 @@ namespace IPA.JsonConverters internal class SemverVersionConverter : JsonConverter { public override Version? ReadJson(JsonReader reader, Type objectType, Version? existingValue, bool hasExistingValue, JsonSerializer serializer) - => reader.Value is string s && Version.TryParse(s, out var version) ? version : existingValue; + => reader.Value is not string s ? existingValue : new Version(s); public override void WriteJson(JsonWriter writer, Version? value, JsonSerializer serializer) {