@@ -915,6 +915,13 @@ public override bool CanConvert(Type typeToConvert)
915915 typeToConvert != typeof ( Newtonsoft . Json . Linq . JObject ) ,
916916 $ "Type { typeToConvert } should be handled by its dedicated converter") ;
917917
918+ // Version: STJ treats as scalar (Kind=None) and serializes as string.
919+ // Serialize as object with properties instead.
920+ if ( typeToConvert == typeof ( Version ) )
921+ {
922+ return true ;
923+ }
924+
918925 // Check if STJ handles this type as a scalar
919926 var typeInfo = JsonSerializerOptions . Default . GetTypeInfo ( typeToConvert ) ;
920927 return typeInfo . Kind != JsonTypeInfoKind . None ;
@@ -1066,9 +1073,25 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp
10661073
10671074 // Use JsonTypeInfo to enumerate properties - leverages STJ caching and handles JsonIgnore automatically
10681075 var typeInfo = JsonSerializerOptions . Default . GetTypeInfo ( value . GetType ( ) ) ;
1069- foreach ( var propInfo in typeInfo . Properties )
1076+ if ( typeInfo . Properties . Count > 0 )
10701077 {
1071- WriteProperty ( writer , value , propInfo , options ) ;
1078+ foreach ( var propInfo in typeInfo . Properties )
1079+ {
1080+ WriteProperty ( writer , value , propInfo , options ) ;
1081+ }
1082+ }
1083+ else
1084+ {
1085+ // Fallback for types like Version where STJ treats as scalar (Kind=None)
1086+ // Use PSObject Adapted properties to enumerate via reflection
1087+ var pso = PSObject . AsPSObject ( value ) ;
1088+ foreach ( var prop in pso . Properties )
1089+ {
1090+ if ( prop . MemberType == PSMemberTypes . Property )
1091+ {
1092+ WriteAdaptedProperty ( writer , value , prop , options ) ;
1093+ }
1094+ }
10721095 }
10731096
10741097 writer . WriteEndObject ( ) ;
@@ -1105,6 +1128,31 @@ private void WriteProperty(Utf8JsonWriter writer, object obj, JsonPropertyInfo p
11051128 JsonSerializerHelper . WriteValue ( writer , value , options ) ;
11061129 }
11071130 }
1131+
1132+ private void WriteAdaptedProperty ( Utf8JsonWriter writer , object obj , PSPropertyInfo prop , JsonSerializerOptions options )
1133+ {
1134+ object ? value = null ;
1135+ try
1136+ {
1137+ value = prop . Value ;
1138+ }
1139+ catch
1140+ {
1141+ // Property access threw - value remains null
1142+ }
1143+
1144+ writer . WritePropertyName ( prop . Name ) ;
1145+
1146+ // If maxDepth is 0 and value is non-null non-scalar, convert to string
1147+ if ( _cmdlet . Depth == 0 && value is not null && ! JsonSerializerHelper . IsStjNativeScalarType ( value ) )
1148+ {
1149+ writer . WriteStringValue ( value . ToString ( ) ) ;
1150+ }
1151+ else
1152+ {
1153+ JsonSerializerHelper . WriteValue ( writer , value , options ) ;
1154+ }
1155+ }
11081156 }
11091157
11101158 /// <summary>
@@ -1136,6 +1184,12 @@ public static bool IsStjNativeScalarType(object obj)
11361184 return false ;
11371185 }
11381186
1187+ // Version: STJ treats as scalar (Kind=None) but V1 serializes as object with properties.
1188+ if ( type == typeof ( Version ) )
1189+ {
1190+ return false ;
1191+ }
1192+
11391193 // GetTypeInfo() has internal caching, no need for additional cache
11401194 var typeInfo = JsonSerializerOptions . Default . GetTypeInfo ( type ) ;
11411195 return typeInfo . Kind == JsonTypeInfoKind . None ;
0 commit comments