8using YamlDotNet.Core.Events;
9using YamlDotNet.Serialization;
26 ArgumentNullException.ThrowIfNull(type);
28 var supported = type == typeof(global::System.Version);
29 if (!supported && validate)
30 throw new NotSupportedException($
"{nameof(VersionConverter)} does not convert {type}s!");
36 public override void WriteJson(JsonWriter? writer,
object? value, JsonSerializer serializer)
38 ArgumentNullException.ThrowIfNull(writer);
44 else if (value is global::System.Version version)
46 writer.WriteValue(version.Semver().ToString());
50 throw new ArgumentException(
"Expected Version object!", nameof(value));
55 public override object?
ReadJson(JsonReader? reader, Type? objectType,
object? existingValue, JsonSerializer serializer)
57 ArgumentNullException.ThrowIfNull(reader);
61 if (reader.TokenType == JsonToken.Null)
64 if (reader.TokenType == JsonToken.String)
68 var v = global::System.Version.Parse((
string)reader.Value!);
73 throw new JsonSerializationException($
"Error parsing version string: {reader.Value}", ex);
77 throw new JsonSerializationException(
78 $
"Unexpected token or value when parsing version. Token: {reader.TokenType}, Value: {reader.Value}");
88 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) =>
throw new NotSupportedException(
"Deserialization not supported!");
91 public void WriteYaml(IEmitter? emitter,
object? value, Type type, ObjectSerializer serializer)
93 ArgumentNullException.ThrowIfNull(emitter);
98 throw new NotSupportedException(
"Null values not supported!");
100 var version = (global::System.Version)value;
JsonConverter and IYamlTypeConverter for serializing global::System.Versions in semver format.
static bool CheckSupportsType(Type? type, bool validate)
Check if the VersionConverter supports (de)serializing a given type .
object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
override void WriteJson(JsonWriter? writer, object? value, JsonSerializer serializer)
override bool CanConvert(Type objectType)
void WriteYaml(IEmitter? emitter, object? value, Type type, ObjectSerializer serializer)
override? object ReadJson(JsonReader? reader, Type? objectType, object? existingValue, JsonSerializer serializer)