| | | 1 | | # Portions derived from PowerShell-Yaml (https://github.com/cloudbase/powershell-yaml) |
| | | 2 | | # Copyright (c) 2016–2024 Cloudbase Solutions Srl |
| | | 3 | | # Licensed under the Apache License, Version 2.0 (Apache-2.0). |
| | | 4 | | # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| | | 5 | | # Modifications Copyright (c) 2025 Kestrun Contributors |
| | | 6 | | |
| | | 7 | | <# |
| | | 8 | | .SYNOPSIS |
| | | 9 | | Convert a PSObject to a generic object, converting any nested hashtables or lists as well. |
| | | 10 | | .DESCRIPTION |
| | | 11 | | This function takes a PSObject as input and converts it to a generic object. It ensures that any nested hashtables o |
| | | 12 | | .PARAMETER InputObject |
| | | 13 | | The PSObject to convert. |
| | | 14 | | .EXAMPLE |
| | | 15 | | $psObj = [PSCustomObject]@{ Key1 = "Value1"; Key2 = @( "Val2a", "Val2b" ); Key3 = @{ SubKey = "SubVal" } } |
| | | 16 | | $genericObj = Convert-PSObjectToGenericObject -Data $psObj |
| | | 17 | | # $genericObj is now a generic object with the same properties, where Key2 is a List[object] and Key3 is an OrderedD |
| | | 18 | | .NOTES |
| | | 19 | | This function is designed to work with PowerShell 7.0 and above. |
| | | 20 | | #> |
| | | 21 | | function Convert-PSObjectToGenericObject { |
| | | 22 | | param( |
| | | 23 | | [Parameter(Mandatory = $false)] |
| | | 24 | | [System.Object]$InputObject |
| | | 25 | | ) |
| | | 26 | | |
| | 1 | 27 | | if ($null -eq $InputObject) { |
| | 1 | 28 | | return $InputObject |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | # Use PowerShell's -is operator for efficient type checking |
| | 1 | 32 | | if ($InputObject -is [System.Collections.Specialized.OrderedDictionary]) { |
| | 1 | 33 | | return Convert-OrderedHashtableToDictionary $InputObject |
| | 1 | 34 | | } elseif ($InputObject -is [System.Collections.IDictionary]) { |
| | 1 | 35 | | return Convert-HashtableToDictionary $InputObject |
| | 1 | 36 | | } elseif ($InputObject -is [System.Collections.IList]) { |
| | 1 | 37 | | return Convert-ListToGenericList $InputObject |
| | | 38 | | } |
| | 1 | 39 | | return $InputObject |
| | | 40 | | } |