@@ -33,6 +33,26 @@ public override bool EnterMapping(IObjectDescriptor key, IObjectDescriptor value
3333 }
3434}
3535
36+ internal static class PSObjectHelper {
37+ /// <summary>
38+ /// Unwraps a PSObject to its BaseObject if the BaseObject is not a PSCustomObject.
39+ /// </summary>
40+ /// <param name="obj">The object to potentially unwrap</param>
41+ /// <param name="unwrappedType">The type of the unwrapped object</param>
42+ /// <returns>The unwrapped object if it was a PSObject wrapping a non-PSCustomObject, otherwise the original object</returns>
43+ public static object UnwrapIfNeeded ( object obj , out Type unwrappedType ) {
44+ if ( obj is PSObject psObj && psObj . BaseObject != null ) {
45+ var baseType = psObj . BaseObject . GetType ( ) ;
46+ if ( baseType != typeof ( System . Management . Automation . PSCustomObject ) ) {
47+ unwrappedType = baseType ;
48+ return psObj . BaseObject ;
49+ }
50+ }
51+ unwrappedType = obj ? . GetType ( ) ;
52+ return obj ;
53+ }
54+ }
55+
3656public class BigIntegerTypeConverter : IYamlTypeConverter {
3757 public bool Accepts ( Type type ) {
3858 return typeof ( BigInteger ) . IsAssignableFrom ( type ) ;
@@ -84,20 +104,8 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize
84104 continue ;
85105 }
86106 serializer ( entry . Key , entry . Key . GetType ( ) ) ;
87- var objType = entry . Value . GetType ( ) ;
88- var val = entry . Value ;
89- if ( entry . Value is PSObject nestedObj ) {
90- if ( nestedObj . BaseObject != null ) {
91- var nestedType = nestedObj . BaseObject . GetType ( ) ;
92- if ( nestedType != typeof ( System . Management . Automation . PSCustomObject ) ) {
93- objType = nestedObj . BaseObject . GetType ( ) ;
94- val = nestedObj . BaseObject ;
95- }
96- }
97- serializer ( val , objType ) ;
98- } else {
99- serializer ( entry . Value , entry . Value . GetType ( ) ) ;
100- }
107+ var unwrapped = PSObjectHelper . UnwrapIfNeeded ( entry . Value , out var unwrappedType ) ;
108+ serializer ( unwrapped , unwrappedType ) ;
101109 }
102110 emitter . Emit ( new MappingEnd ( ) ) ;
103111 }
@@ -126,7 +134,9 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria
126134
127135 public void WriteYaml ( IEmitter emitter , object value , Type type , ObjectSerializer serializer ) {
128136 var psObj = ( PSObject ) value ;
129- if ( ! typeof ( IDictionary ) . IsAssignableFrom ( psObj . BaseObject . GetType ( ) ) && ! typeof ( PSCustomObject ) . IsAssignableFrom ( psObj . BaseObject . GetType ( ) ) ) {
137+ if ( psObj . BaseObject != null &&
138+ ! typeof ( IDictionary ) . IsAssignableFrom ( psObj . BaseObject . GetType ( ) ) &&
139+ ! typeof ( PSCustomObject ) . IsAssignableFrom ( psObj . BaseObject . GetType ( ) ) ) {
130140 serializer ( psObj . BaseObject , psObj . BaseObject . GetType ( ) ) ;
131141 return ;
132142 }
@@ -141,19 +151,8 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize
141151 emitter . Emit ( new Scalar ( AnchorName . Empty , "tag:yaml.org,2002:null" , "" , ScalarStyle . Plain , true , false ) ) ;
142152 } else {
143153 serializer ( prop . Name , prop . Name . GetType ( ) ) ;
144- var objType = prop . Value . GetType ( ) ;
145- var val = prop . Value ;
146- if ( prop . Value is PSObject nestedPsObj ) {
147- if ( nestedPsObj . BaseObject != null ) {
148- var nestedType = nestedPsObj . BaseObject . GetType ( ) ;
149- if ( nestedType != typeof ( System . Management . Automation . PSCustomObject ) ) {
150- objType = nestedPsObj . BaseObject . GetType ( ) ;
151- val = nestedPsObj . BaseObject ;
152- }
153- }
154- }
155- serializer ( val , objType ) ;
156-
154+ var unwrapped = PSObjectHelper . UnwrapIfNeeded ( prop . Value , out var unwrappedType ) ;
155+ serializer ( unwrapped , unwrappedType ) ;
157156 }
158157 }
159158 emitter . Emit ( new MappingEnd ( ) ) ;
0 commit comments