| | | 1 | | using System.Collections; |
| | | 2 | | using System.Management.Automation; |
| | | 3 | | using YamlDotNet.Core; |
| | | 4 | | using YamlDotNet.Core.Events; |
| | | 5 | | using YamlDotNet.Serialization; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Utilities.Yaml; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// YAML type converter for IDictionary types |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Constructor |
| | | 14 | | /// </remarks> |
| | | 15 | | /// <param name="omitNullValues">If true, null values will be omitted from the output</param> |
| | | 16 | | /// <param name="useFlowStyle">If true, the mapping will be emitted in flow style</param> |
| | 21 | 17 | | public class IDictionaryTypeConverter(bool omitNullValues = false, bool useFlowStyle = false) : IYamlTypeConverter |
| | | 18 | | { |
| | 21 | 19 | | private readonly bool omitNullValues = omitNullValues; |
| | 21 | 20 | | private readonly bool useFlowStyle = useFlowStyle; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Check if the type is IDictionary |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="type">The type to check</param> |
| | | 26 | | /// <returns>true if the type is IDictionary; otherwise, false.</returns> |
| | 111 | 27 | | public bool Accepts(Type type) => typeof(IDictionary).IsAssignableFrom(type); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Read an IDictionary from YAML |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="parser">The YAML parser</param> |
| | | 33 | | /// <param name="type">The type of the object to deserialize</param> |
| | | 34 | | /// <param name="rootDeserializer">The root deserializer</param> |
| | | 35 | | /// <returns>The deserialized object</returns> |
| | | 36 | | public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) |
| | | 37 | | { |
| | 0 | 38 | | var deserializedObject = rootDeserializer(typeof(IDictionary<string, object>)) as IDictionary; |
| | | 39 | | // Ensure a non-null IDictionary is returned to satisfy nullable reference expectations. |
| | 0 | 40 | | return deserializedObject ?? new Hashtable(); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Write an IDictionary to YAML |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="emitter">The YAML emitter</param> |
| | | 47 | | /// <param name="value">The IDictionary to serialize</param> |
| | | 48 | | /// <param name="type">The type of the object to serialize</param> |
| | | 49 | | /// <param name="serializer">The object serializer</param> |
| | | 50 | | public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) |
| | | 51 | | { |
| | 3 | 52 | | if (value == null) |
| | | 53 | | { |
| | | 54 | | // Emit explicit YAML null for a null dictionary value |
| | 0 | 55 | | emitter.Emit(new Scalar(AnchorName.Empty, "tag:yaml.org,2002:null", "", ScalarStyle.Plain, true, false)); |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | var hObj = (IDictionary)value; |
| | 3 | 60 | | var mappingStyle = useFlowStyle ? MappingStyle.Flow : MappingStyle.Block; |
| | | 61 | | |
| | 3 | 62 | | emitter.Emit(new MappingStart(AnchorName.Empty, TagName.Empty, true, mappingStyle)); |
| | 20 | 63 | | foreach (DictionaryEntry entry in hObj) |
| | | 64 | | { |
| | 7 | 65 | | if (entry.Value == null) |
| | | 66 | | { |
| | 3 | 67 | | if (omitNullValues) |
| | | 68 | | { |
| | | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | // Emit a blank plain scalar (no quotes) to represent YAML null for dictionary entries (implicit null, n |
| | 2 | 72 | | serializer(entry.Key, entry.Key.GetType()); |
| | | 73 | | // Must set at least one implicit flag to true when tag is empty; plain implicit=true allows empty scala |
| | 2 | 74 | | emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, string.Empty, ScalarStyle.Plain, true, false)); |
| | 2 | 75 | | continue; |
| | | 76 | | } |
| | 4 | 77 | | serializer(entry.Key, entry.Key.GetType()); |
| | 4 | 78 | | var objType = entry.Value.GetType(); |
| | 4 | 79 | | var val = entry.Value; |
| | | 80 | | // If empty string, emit explicitly as double-quoted to satisfy tests distinguishing from null blank. |
| | 4 | 81 | | if (val is string s && s.Length == 0) |
| | | 82 | | { |
| | 2 | 83 | | emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, string.Empty, ScalarStyle.DoubleQuoted, true, f |
| | 2 | 84 | | continue; |
| | | 85 | | } |
| | 2 | 86 | | if (entry.Value is PSObject nestedObj) |
| | | 87 | | { |
| | 0 | 88 | | if (nestedObj.BaseObject == null) |
| | | 89 | | { |
| | 0 | 90 | | if (omitNullValues) |
| | | 91 | | { |
| | | 92 | | continue; |
| | | 93 | | } |
| | | 94 | | // Emit a blank plain scalar (no quotes) to represent YAML null for dictionary entries (implicit nul |
| | 0 | 95 | | serializer(entry.Key, entry.Key.GetType()); |
| | 0 | 96 | | emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, string.Empty, ScalarStyle.Plain, true, fals |
| | 0 | 97 | | continue; |
| | | 98 | | } |
| | 0 | 99 | | var nestedType = nestedObj.BaseObject.GetType(); |
| | 0 | 100 | | if (nestedType != typeof(PSCustomObject)) |
| | | 101 | | { |
| | 0 | 102 | | objType = nestedObj.BaseObject.GetType(); |
| | 0 | 103 | | val = nestedObj.BaseObject; |
| | | 104 | | } |
| | 0 | 105 | | serializer(val, objType); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 2 | 109 | | serializer(entry.Value, entry.Value.GetType()); |
| | | 110 | | } |
| | | 111 | | } |
| | 3 | 112 | | emitter.Emit(new MappingEnd()); |
| | 3 | 113 | | } |
| | | 114 | | } |