Repository navigation
ConvertFrom/To-Hashtable and/or ConvertTo-PSCustomObject cmdlets #159
Description
Activity
Are we really trying to convert between hashtables and objects, or is this for serialization?
That is, do you want actual hashtables, or string representation of hashtables?If it's the later, a very robust version of this already exists in the Configuration module (specifically, in it's MetaData submodule, but exported by Configuration).
If you want recursive conversion of live objects, I could extract that into a function and expose it, but it's currently not accessible.
Reacted by Evan ReedI think the need here is for:
- An API to convert an object in PowerShell to PowerShell hashtable literal string notation (PSD syntax). Mostly with actual System.Collections.Hashtable objects, but could see use in other cases
- Another API to convert from the string back to a PSCustomObject or S.C.Hashtable.
The only complexity I see here is that PSD can have dynamic bits in it (
@{ X = if (2 -gt 4) { 'Hi' } else { 'Bye' } }) since a PSD1 file is parsed in Restricted Language Mode, but otherwise I think this is basically a call for a PSD-syntax version of ConvertTo/From-JsonReacted by Rain Sallow (/u/ta11ow), Thomas Nieto and Michael KlementThe only complexity I see here is that PSD can have dynamic bits in it (@{ X = if (2 -gt 4) { 'Hi' } else { 'Bye' } }) since a PSD1 file is parsed in Restricted Language Mode, but otherwise I think this is basically a call for a PSD-syntax version of ConvertTo/From-Json
Seeing this again, this part is wrong. The Restricted Language Mode is only true of module manifests. However, it does raise the question of whether a module manifest with such dynamic fields should be expected to parse with one of the proposed cmdlets
vexx32 commented
on Jul 30, 2020 ContributorMore actionsCould always have a
-Strictor-RestrictedLanguageswitch on the cmdlet to enable that option.Could always have a -Strict or -RestrictedLanguage switch on the cmdlet to enable that option.
Yeah the latter is what I'm imagining. I already wrote a little parser for the strict version. I just need to publish it somehow... The RLM version means taking a dependency on the PowerShell runtime, which I'm less keen on, but should be ok in the cmdlet.
I'd like to break it up into the core library and the cmdlet, with the cmdlet able to also parse RLM
Reacted by Rain Sallow (/u/ta11ow)
Related discussion in #109, specifically this comment:
We think the scenario we're trying to address is primarily casting nested hashtables of hashtables into PSCustomObjects (or possibly even other types).