tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
VersionConverter.cs
Go to the documentation of this file.
1using System;
2
3using Newtonsoft.Json;
4
6
7using YamlDotNet.Core;
8using YamlDotNet.Core.Events;
9using YamlDotNet.Serialization;
10
12{
17 {
24 static bool CheckSupportsType(Type? type, bool validate)
25 {
26 ArgumentNullException.ThrowIfNull(type);
27
28 var supported = type == typeof(global::System.Version);
29 if (!supported && validate)
30 throw new NotSupportedException($"{nameof(VersionConverter)} does not convert {type}s!");
31
32 return supported;
33 }
34
36 public override void WriteJson(JsonWriter? writer, object? value, JsonSerializer serializer)
37 {
38 ArgumentNullException.ThrowIfNull(writer);
39
40 if (value == null)
41 {
42 writer.WriteNull();
43 }
44 else if (value is global::System.Version version)
45 {
46 writer.WriteValue(version.Semver().ToString());
47 }
48 else
49 {
50 throw new ArgumentException("Expected Version object!", nameof(value));
51 }
52 }
53
55 public override object? ReadJson(JsonReader? reader, Type? objectType, object? existingValue, JsonSerializer serializer)
56 {
57 ArgumentNullException.ThrowIfNull(reader);
58
59 CheckSupportsType(objectType, true);
60
61 if (reader.TokenType == JsonToken.Null)
62 return null;
63
64 if (reader.TokenType == JsonToken.String)
65 {
66 try
67 {
68 var v = global::System.Version.Parse((string)reader.Value!);
69 return v.Semver();
70 }
71 catch (Exception ex)
72 {
73 throw new JsonSerializationException($"Error parsing version string: {reader.Value}", ex);
74 }
75 }
76
77 throw new JsonSerializationException(
78 $"Unexpected token or value when parsing version. Token: {reader.TokenType}, Value: {reader.Value}");
79 }
80
82 public override bool CanConvert(Type objectType) => CheckSupportsType(objectType, false);
83
85 public bool Accepts(Type type) => CheckSupportsType(type, false);
86
88 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) => throw new NotSupportedException("Deserialization not supported!"); // The default implementation is fine at handling this
89
91 public void WriteYaml(IEmitter? emitter, object? value, Type type, ObjectSerializer serializer)
92 {
93 ArgumentNullException.ThrowIfNull(emitter);
94
95 CheckSupportsType(type, true);
96
97 if (value == null)
98 throw new NotSupportedException("Null values not supported!");
99
100 var version = (global::System.Version)value;
101 emitter.Emit(
102 new Scalar(
103 version
104 .Semver()
105 .ToString()));
106 }
107 }
108}
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)