jborean93 / PSToml

PowerShell TOML Parser and Writer
MIT License
44 stars 6 forks source link

Add OrderedHashtable support #9

Open josephwhite opened 1 month ago

josephwhite commented 1 month ago

Similar to ConvertFrom-Json, it would be nice to have parameter to natively cast the OrderedDictionary to an OrderedHashtable type.

# EZPZ typecast from OrderedDictionary to Hashtable
# https://stackoverflow.com/a/48679838
$obj = [hashtable](Get-Content -Path $Path | ConvertFrom-Toml)
<#
  Observations on how TOML, JSON, and PSD1 are imported as objects and how to convert to a Hashtable base type if possible.
  TOML -> OrderedDictionary
       -> OrderedDictionary -> Hashtable (https://stackoverflow.com/a/48679838)
  JSON -> PSCustomObject
       -> OrderedHashtable -> Hashtable (Using -AsHashtable in PowerShell v7.3.0-preview.6+)
       -> PSCustomObject -> Hashtable (https://stackoverflow.com/a/32102005)
  PDS1 -> Hashtable
#>
jborean93 commented 1 month ago

While this is certainly possible is the reason for wanting this because some cmdlets you want to provide the data to only support [Hashtable] rather than [IDictionary]? Seems like it would be more flexible to change the latter if you are in control of them so it doesn't care what dictionary type is supplied.

Also keep in mind OrderedHashtable is only available in 7.3+, older PowerShell versions will not be able to use it.