GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
CsvValueYamlConverter.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.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Threading.Tasks;
22using YamlDotNet.Core;
23using YamlDotNet.Core.Events;
24using YamlDotNet.Serialization;
25
26namespace GenerallyPositive
27{
28 // written by grok
29 public class CsvValue
30 {
31 public string Value { get; set; }
32 public bool WasQuoted { get; set; }
33
34 public CsvValue(string value, bool wasQuoted)
35 {
36 Value = value;
37 WasQuoted = wasQuoted;
38 }
39 }
40
41 internal class CsvValueYamlConverter : IYamlTypeConverter
42 {
43 public bool Accepts(Type type) => type == typeof(CsvValue);
44
45 public object ReadYaml(IParser parser, Type type)
46 {
47 var value = parser.Consume<Scalar>().Value;
48 bool wasQuoted = value.StartsWith("\"") && value.EndsWith("\"");
49 if (wasQuoted) value = value.Trim('"');
50 return new CsvValue(value, wasQuoted);
51 }
52
53 public void WriteYaml(IEmitter emitter, object value, Type type)
54 {
55 var csvValue = (CsvValue)value;
56 var scalarStyle = csvValue.WasQuoted ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain;
57 emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, csvValue.Value, scalarStyle, !csvValue.WasQuoted, csvValue.WasQuoted));
58 }
59
60 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
61 {
62 var scalarValue = parser.Consume<Scalar>().Value;
63 bool wasQuoted = scalarValue.StartsWith("\"") && scalarValue.EndsWith("\"");
64 if (wasQuoted) scalarValue = scalarValue.Trim('"');
65 return new CsvValue(scalarValue, wasQuoted);
66 }
67
68 public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
69 {
70 var csvValue = (CsvValue)value;
71 var scalarStyle = csvValue.WasQuoted ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain;
72 emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, csvValue.Value, scalarStyle, !csvValue.WasQuoted, csvValue.WasQuoted));
73 }
74 }
75}
76