GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
QuotedStringYamlConverter.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 internal class QuotedString
29 {
30 public string Value { get; set; }
31 public bool WasQuoted { get; set; }
32
33 public QuotedString(string value, bool wasQuoted)
34 {
35 Value = value;
36 WasQuoted = wasQuoted;
37 }
38 }
39
40 internal class QuotedStringYamlConverter : IYamlTypeConverter
41 {
42 public bool Accepts(Type type) => type == typeof(QuotedString);
43
44 public object ReadYaml(IParser parser, Type type)
45 {
46 var value = parser.Consume<Scalar>().Value;
47 bool wasQuoted = value.StartsWith("\"") && value.EndsWith("\"");
48 if (wasQuoted) value = value.Trim('"');
49 return new QuotedString(value, wasQuoted);
50 }
51
52 public void WriteYaml(IEmitter emitter, object value, Type type)
53 {
54 var quotedString = (QuotedString)value;
55 var scalarStyle = quotedString.WasQuoted ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain;
56 emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, quotedString.Value, scalarStyle, !quotedString.WasQuoted, quotedString.WasQuoted));
57 }
58
59 public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
60 {
61 var scalarValue = parser.Consume<Scalar>().Value;
62 bool wasQuoted = scalarValue.StartsWith("\"") && scalarValue.EndsWith("\"");
63 if (wasQuoted) scalarValue = scalarValue.Trim('"');
64 return new QuotedString(scalarValue, wasQuoted);
65 }
66
67 public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
68 {
69 var quotedString = (QuotedString)value;
70 var scalarStyle = quotedString.WasQuoted ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain;
71 emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, quotedString.Value, scalarStyle, !quotedString.WasQuoted, quotedString.WasQuoted));
72 }
73 }
74}
75