GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
NullableGuidYamlConverter.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
17using System;
18using YamlDotNet.Core;
19using YamlDotNet.Core.Events;
20using YamlDotNet.Serialization;
21
26public class NullableGuidYamlConverter : IYamlTypeConverter
27{
28 public bool Accepts(Type type)
29 {
30 return type == typeof(Guid) || typeof(Guid?) == type;
31 }
32
33 public object ReadYaml(IParser parser, Type type)
34 {
35 var scalar = parser.Consume<Scalar>();
36
37 if (string.IsNullOrEmpty(scalar.Value) || scalar.Value.ToLower() == "null")
38 return null;
39
40 return Guid.Parse(scalar.Value);
41 }
42
43 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
44 {
45 var scalarValue = rootDeserializer(typeof(string)) as string;
46
47 if (string.IsNullOrEmpty(scalarValue) || scalarValue.ToLower() == "null")
48 return null;
49
50 return Guid.Parse(scalarValue);
51 }
52
53 public void WriteYaml(IEmitter emitter, object value, Type type)
54 {
55 if (value == null)
56 emitter.Emit(new Scalar("null"));
57 else
58 emitter.Emit(new Scalar(null, null, ((Guid)value).ToString()));
59 }
60
61 public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
62 {
63 if (value == null)
64 emitter.Emit(new Scalar("null"));
65 else
66 serializer(((Guid)value).ToString(), typeof(string));
67 }
68}
Writes a Guid, and a null Guid?, the way the rest of GPAL writes nulls. Without this YamlDotNet has n...