GPAL - Generally Positive Automation Library
v1.0
GPAL The Fluent Automation LIbrary
Toggle main menu visibility
Loading...
Searching...
No Matches
IPAddressYamlConverter.cs
1
// =============================================================================
2
// GPAL - Generally Positive Automation Library
3
// Copyright © 2026 Software Decisions, Inc. All rights reserved.
4
//
5
// This file is part of GPAL.
6
// Licensed under the Business Source License 1.1
7
//
8
// Primary development, architecture, and vision by Michael B. Vederman,
9
// CEO of Software Decisions, Inc., Texas.
10
//
11
// Internal development maintained privately.
12
// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
13
//
14
// See LICENSE for full terms, including Additional Use Grant.
15
// =============================================================================
16
17
using
System;
18
using
System.Net;
19
using
YamlDotNet.Core;
20
using
YamlDotNet.Core.Events;
21
using
YamlDotNet.Serialization;
22
23
public
class
IPAddressYamlConverter
: IYamlTypeConverter
24
{
25
public
bool
Accepts(Type type)
26
{
27
return
type == typeof(IPAddress);
28
}
29
30
public
object
ReadYaml(IParser parser, Type type)
31
{
32
var value = parser.Consume<Scalar>().Value;
33
if
(IPAddress.TryParse(value, out var ipAddress))
34
{
35
return
ipAddress;
36
}
37
return
null
;
// Handle parsing failure accordingly
38
}
39
40
public
void
WriteYaml(IEmitter emitter,
object
value, Type type)
41
{
42
if
(value is IPAddress ipAddress)
43
{
44
emitter.Emit(
new
Scalar(ipAddress.ToString()));
45
}
46
else
47
{
48
emitter.Emit(
new
Scalar(
null
));
// Handle writing failure accordingly
49
}
50
}
51
public
object
ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
52
{
53
// Use the deserializer to extract the scalar value
54
var scalarValue = rootDeserializer(type) as string;
55
if
(scalarValue ==
null
)
56
{
57
throw
new
YamlException(
"Expected a string for IP address."
);
58
}
59
60
IPAddress ipAddress;
61
if
(IPAddress.TryParse(scalarValue, out ipAddress))
62
{
63
return
ipAddress;
64
}
65
66
throw
new
YamlException($
"Invalid IP address format: {scalarValue}"
);
67
}
68
69
public
void
WriteYaml(IEmitter emitter,
object
value, Type type, ObjectSerializer serializer)
70
{
71
if
(value is IPAddress ipAddress)
72
{
73
// Emit the mapping for IP address in YAML format
74
emitter.Emit(
new
MappingStart());
// Start a new mapping
75
76
emitter.Emit(
new
Scalar(
"ipAddress"
));
// Emit the field name
77
serializer(ipAddress.ToString());
// Use the serializer to handle the value
78
79
emitter.Emit(
new
MappingEnd());
// End the mapping
80
}
81
else
if
(value ==
null
)
82
{
83
// a null is data, not a failure. throwing here killed the whole conversion on a null entry in a
84
// collection of IPAddress, and the sibling overload above already writes null for this case
85
emitter.Emit(
new
Scalar(
"null"
));
86
}
87
else
88
{
89
throw
new
ArgumentException(
"Expected an IPAddress object."
, nameof(value));
90
}
91
}
92
}
93
IPAddressYamlConverter
Definition
IPAddressYamlConverter.cs:24
Common
IPAddressYamlConverter.cs
Generated by
1.17.0