You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
717 B

  1. #nullable enable
  2. using System;
  3. using Newtonsoft.Json;
  4. using Version = Hive.Versioning.Version;
  5. namespace IPA.JsonConverters
  6. {
  7. internal class SemverVersionConverter : JsonConverter<Version?>
  8. {
  9. public override Version? ReadJson(JsonReader reader, Type objectType, Version? existingValue, bool hasExistingValue, JsonSerializer serializer)
  10. => reader.Value is string s && Version.TryParse(s, out var version) ? version : existingValue;
  11. public override void WriteJson(JsonWriter writer, Version? value, JsonSerializer serializer)
  12. {
  13. if (value == null) writer.WriteNull();
  14. else writer.WriteValue(value.ToString());
  15. }
  16. }
  17. }