Repository navigation
Closed
Labels
Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Description
Any reason why hidden has no bearing on ConvertTo-Json? Seems that Get-Member honors visibility, but JSON serialization does not.
Steps to reproduce
Class Person {
hidden [string] $FirstName
hidden [string] $LastName
[string] $FullName
Person([string]$First, [string]$Last) {
$this.FirstName = $First
$this.LastName = $Last
$this.FullName = $First, $Last -join " "
}
[string] ToString() {
Return $this.FullName
}
}
$jDoe = [Person]::new("John", "Doe")
Write-Output "`nProperties:"
$jDoe | Get-Member -MemberType "Property" | Format-Table "Name", "MemberType"
Write-Output "`nJSON:"
$jDoe | ConvertTo-JsonExpected behavior
Properties:
Name MemberType
---- ----------
FullName Property
JSON:
{
"FullName": "John Doe"
}
Actual behavior
Properties:
Name MemberType
---- ----------
FullName Property
JSON:
{
"FirstName": "John",
"LastName": "Doe",
"FullName": "John Doe"
}
Environment data
Name Value
---- -----
PSVersion 6.2.1
PSEdition Core
GitCommitId 6.2.1
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
FWIW, Appears ConvertTo-Xml -As "String" does honor visibility:
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="Person">
<Property Name="FullName" Type="System.String">John Doe</Property>
</Object>
</Objects>
Also, did see #6652 and there's some mention re: hidden with regards to format-* but didn't see anythign related to JSON serialization.
Reacted by Michael Klement, Patrick Meinecke, Klaus Frank and Cedric Ahlers
Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
[-]Class hidden properties still serialized[/-][+]Class hidden properties still serialized to JSON[/+]vexx32 commentedon Jun 6, 2019
/cc Mark Kraus (@markekraus) I think you're probably the best person to ask about why this works this way / how we might align it more with other
Convertcmdlets. 🙂Brad-Christie commentedon Jun 6, 2019
Looked quickly, appears XML uses a
CustomSerializationserializer, which appears to iterate the PSObject.properties itself, which I assume implements some kind of filtering mechanism (through thePSMemberInfoIntegratingCollection<>).ConvertTo-Json, however, appears to hand it off to newtonsoft without allowing it the benefit of examining context, therefore it implicitly assembles it via reflection.Looks like you'd have to create a ContractResolver and pass it off that context for it to work the same, at least from what I gathered from doing a preliminary. 🤷♂️
thezim commentedon Jun 11, 2019
Someone correct me if I’m wrong but isn’t serialization/deserialization a misnomer for the Convert* cmdlets? You are never going to get a functional class back out of a “serialized” class unless it a POCO to start with. It’s something different by then in the example here.
Brad-Christie commentedon Jun 11, 2019
Greg Zimmerman (@thezim) I can appreciate that, but not looking to convert back. The example was handy given I ran into the issue, but the original intent is to use a model to build a final object. That object is then consumed by an external system, and I'd rather not expose the intermediary properties.
That said, It's still inconsistent behavior. Get-Member and Csv, Html & Xml work, so (IMHO) ConvertTo-Json should work too.
mklement0 commentedon Nov 17, 2019
Just to affirm that the current behavior is indeed unexpected:
Select-Object(with wildcards) andConvertTo-Csv/Export-Csv- sensibly - do not include hidden properties -ConvertTo-Jsonshould act the same.(
Export-CliXmldoes, but there it makes sense (as-faithful-as-possible serialization); the rarely usedConvertTo-Xmldoes not).iSazonov commentedon Nov 26, 2019
May be fixed in #11198 as a side effect. :-)
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.
nosalan commentedon Nov 27, 2023
It is labeled as a bug, it should be reopened.
microsoft-github-policy-service commentedon May 28, 2024
This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.
Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.
dbramblett commentedon Nov 15, 2024
I just ran into this as a problem as well. I create objects which turned out to have some hidden properties, which are in turn converted to json and exported. Because ConvertTo-Json does not behave the same way as Get-Member, ConvertTo-Csv, etc., it took me longer than it should have to track down why a property that didn't appear to be part of any of the code anywhere I'd written, was popping up in the json output and creating problems when I tried to import that content back into powershell somewhere down the road. As nosalan remarked, this is a bug and should be re-opened.