tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Public Member Functions | Static Private Member Functions | List of all members
Tgstation.Server.Shared.VersionConverter Class Referencesealed

JsonConverter and IYamlTypeConverter for serializing global::System.Versions in semver format. More...

Inheritance diagram for Tgstation.Server.Shared.VersionConverter:
Inheritance graph
[legend]
Collaboration diagram for Tgstation.Server.Shared.VersionConverter:
Collaboration graph
[legend]

Public Member Functions

override void WriteJson (JsonWriter? writer, object? value, JsonSerializer serializer)
 
override? object ReadJson (JsonReader? reader, Type? objectType, object? existingValue, JsonSerializer serializer)
 
override bool CanConvert (Type objectType)
 
bool Accepts (Type type)
 
object ReadYaml (IParser parser, Type type, ObjectDeserializer rootDeserializer)
 
void WriteYaml (IEmitter? emitter, object? value, Type type, ObjectSerializer serializer)
 

Static Private Member Functions

static bool CheckSupportsType (Type? type, bool validate)
 Check if the VersionConverter supports (de)serializing a given type .
 

Detailed Description

JsonConverter and IYamlTypeConverter for serializing global::System.Versions in semver format.

Definition at line 16 of file VersionConverter.cs.

Member Function Documentation

◆ Accepts()

bool Tgstation.Server.Shared.VersionConverter.Accepts ( Type  type)

◆ CanConvert()

override bool Tgstation.Server.Shared.VersionConverter.CanConvert ( Type  objectType)

◆ CheckSupportsType()

static bool Tgstation.Server.Shared.VersionConverter.CheckSupportsType ( Type?  type,
bool  validate 
)
staticprivate

Check if the VersionConverter supports (de)serializing a given type .

Parameters
typeThe Type to check.
validateIf the method should throw if validation fails.
Returns
true if type is a global::System.Version, false otherwise.

Definition at line 24 of file VersionConverter.cs.

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 }

Referenced by Tgstation.Server.Shared.VersionConverter.ReadJson(), and Tgstation.Server.Shared.VersionConverter.WriteYaml().

Here is the caller graph for this function:

◆ ReadJson()

override? object Tgstation.Server.Shared.VersionConverter.ReadJson ( JsonReader?  reader,
Type?  objectType,
object?  existingValue,
JsonSerializer  serializer 
)

Definition at line 55 of file VersionConverter.cs.

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 }
static bool CheckSupportsType(Type? type, bool validate)
Check if the VersionConverter supports (de)serializing a given type .

References Tgstation.Server.Shared.VersionConverter.CheckSupportsType().

Here is the call graph for this function:

◆ ReadYaml()

object Tgstation.Server.Shared.VersionConverter.ReadYaml ( IParser  parser,
Type  type,
ObjectDeserializer  rootDeserializer 
)

◆ WriteJson()

override void Tgstation.Server.Shared.VersionConverter.WriteJson ( JsonWriter?  writer,
object?  value,
JsonSerializer  serializer 
)

Definition at line 36 of file VersionConverter.cs.

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 }

◆ WriteYaml()

void Tgstation.Server.Shared.VersionConverter.WriteYaml ( IEmitter?  emitter,
object?  value,
Type  type,
ObjectSerializer  serializer 
)

Definition at line 91 of file VersionConverter.cs.

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 }

References Tgstation.Server.Shared.VersionConverter.CheckSupportsType().

Here is the call graph for this function:

The documentation for this class was generated from the following file: