GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
NullableBigIntegerYamlConverter.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 System.Numerics;
19using YamlDotNet.Core;
20using YamlDotNet.Core.Events;
21using YamlDotNet.Serialization;
22
23public class NullableBigIntegerYamlConverter : IYamlTypeConverter
24{
25 public bool Accepts(Type type)
26 {
27 return type == typeof(BigInteger) || typeof(BigInteger?) == type;
28 }
29
30 public object ReadYaml(IParser parser, Type type)
31 {
32 var scalar = parser.Consume<Scalar>();
33 if (string.IsNullOrEmpty(scalar.Value) || scalar.Value.ToLower() == "null")
34 {
35 return null; // Handle null or empty string as null
36 }
37 return BigInteger.Parse(scalar.Value);
38 }
39
40 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
41 {
42 // Deserialize the scalar value (which should be a string representation of the number)
43 var scalarValue = rootDeserializer(typeof(string)) as string;
44
45 if (string.IsNullOrEmpty(scalarValue) || scalarValue.ToLower() == "null")
46 {
47 return null; // Handle null or empty string as null
48 }
49
50 // Parse the BigInteger from the string
51 return BigInteger.Parse(scalarValue);
52 }
53
54 public void WriteYaml(IEmitter emitter, object value, Type type)
55 {
56 if (value == null)
57 emitter.Emit(new Scalar("null"));
58 else
59 emitter.Emit(new Scalar(null, null, ((BigInteger)value).ToString()));
60 }
61
62 public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
63 {
64 if (value == null)
65 emitter.Emit(new Scalar("null"));
66 else
67 serializer(((BigInteger)value).ToString(), typeof(string));
68 }
69
70}
71