GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
ComplexYamlConverter.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;
20using YamlDotNet.Core;
21using YamlDotNet.Core.Events;
22using YamlDotNet.Serialization;
23
24public class ComplexYamlConverter : IYamlTypeConverter
25{
26 public bool Accepts(Type type)
27 {
28 bool retVal = type == typeof(Complex);
29 return retVal;
30 }
31
32 public object ReadYaml(IParser parser, Type type)
33 {
34 var scalar = parser.Consume<Scalar>();
35 return ConverterHelper.ParseComplex(scalar.ToString());
36 }
37
38 public void WriteYaml(IEmitter emitter, object value, Type type)
39 {
40 var complexNumber = (Complex)value;
41 string separator = (complexNumber.Imaginary >= 0 ? "+" : "-");
42 emitter.Emit(new Scalar(null, null, $@"{complexNumber.Real}{separator}{Math.Abs(complexNumber.Imaginary)}i"));
43 }
44
45 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
46 {
47 var scalar = parser.Consume<Scalar>();
48 return ConverterHelper.ParseComplex(scalar.Value);
49 }
50
51 public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
52 {
53 var complexNumber = (Complex)value;
54
55 string separator = (complexNumber.Imaginary >= 0 ? "+" : "-");
56 emitter.Emit(new Scalar(null, null, $"{complexNumber.Real}{separator}{Math.Abs(complexNumber.Imaginary)}i"));
57
58 // this saves as
59 // real: 123
60 // imaginary: 123i
61 // we want real+imaginary, easier to parse :)
62 //emitter.Emit(new MappingStart());
63
64 //emitter.Emit(new Scalar(null, "real"));
65 //serializer(complexNumber.Real);
66
67 //emitter.Emit(new Scalar(null, "imaginary"));
68 //serializer(complexNumber.Imaginary);
69
70 //emitter.Emit(new MappingEnd());
71 }
72
73}
74