New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Objects are situationally invisibly [psobject]-wrapped, sometimes causing unexpected behavior. #5579
Comments
This is also observable in PS 5.1
|
@san7hos: That's actually a slightly different problem that only affects Windows PowerShell at this point: the problem is caused by an ETS In your example, the nested array does not have a In my example above, the # .foo as a regular array: works as expected in both PS Core and Windows PowerShell
PS> [pscustomobject] @{ foo = 0, 0 } | ConvertTo-Json -Compress
{"foo":[0,0]} # OK
# .foo being extra-[psobject]-wrapped: serialized via ETS definition for [System.Array],
# which only exists in *Windows PowerShell* at this point,
# so you'll only see the symptom there.
PS> [pscustomobject] @{ foo = New-Object int[] 2 } | ConvertTo-Json -Compress
{"foo":{"value":[0,0],"Count":2}} # !! Extraneous .Count, .value properties |
There are (at least?) 5 basic scenarios in which objects / properties end up invisibly
[psobject]
-wrapped, which can lead to subtle differences in behavior:Note: By
[psobject]
-[extra-]wrapped I mean an object for which-is [psobject]
returns$true
.$_
/$PSItem
or an[object]
or untyped parameter - but not if passed as an argument:[psobject]
-wrapped.).ForEach()
and.Where()
methods are always[psobject]
-wrapped (implied by the output collection type,System.Collections.ObjectModel.Collection<PSObject>
):[psobject]
- for which there is no need, and in most scenarios this will have no practical implications; more surprisingly, however, the same applies to[pscustomobject]
, given that these type accelerators are effectively the same and both refer to[System.Management.Automation.PSObject]
(see Why is [pscustomobject] the same as [psobject], even though a distinct [System.Management.Automation.PSCustomObject] type exists? #4344).As for real-world ramifications (in addition to the difference in
Get-Member
representation - see below):In Windows PowerShell,
ConvertTo-Json
is affected, as originally reportedin ConvertFrom-Json doesn't unwrap arrays on output, resulting in an extraneous wrapper JSON object with
Count
andValue
properties on reconversion #3153 - see below.(PowerShell Core is no longer affected, because that issue has been fixed
via Remove System.Array type data #3231)
Preference variable $OutputEncoding in child scopes misbehaves with encodings created with New-Object / commands rather than expressions #5763
The -f operator doesn't recognize a [psobject]-wrapped array #14355
Array.Sort method call situationally ignores string-comparer argument #14829
Invoke-Command -SSHConnection breaks if the HostName string is [psobject]-wrapped #10687 [fixed in v7.0.0-preview.5]
Conversion of [datetime] property in class does not work #20401 (comment)
Export-Csv
/ConvertTo-Csv
: Stringification of array-valued property values depends on whether they happen to be[psobject]
-wrapped #20450Splatting with arrays containing
--%
, the stop-parsing token, doesn't work if the array element containing that token happens to be[psobject]
-wrapped #21260An incidentally
[psobject]
-wrapped strongly typed array isn't recognized as such in argument-based parameter binding #21496$_
in aForEach-Object
script block reflects each input object (extra-)[psobject]
-wrapped, causing-is [pscustomobject]
tests to become meaningless, as then any input object passes the test - see this SO questionPassing an incidentally wrapped
[psobject]
-wrapped instance to an external serialization API such as[Newtonsoft.Json.JsonConvert]::SerializeObject()
causes unexpected behavior - see this SO post.Note that even non-wrapped instances in the sense above DO have ETS-defined properties (e.g.,
[datetime]::now.psextended
reveals the.DateTime
property).(While #4347 may sound related, the distinct issue there is that additional properties may be added to outputs by provider cmdlets.)
Examples:
Environment data
Current as of:
The text was updated successfully, but these errors were encountered: