Skip to content

Class hidden properties still serialized to JSON #9847

Closed

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-Json

Expected 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.

Activity

  1. added
    Issue-Question
    on Jun 6, 2019
  2. changed the title [-]Class hidden properties still serialized[/-] [+]Class hidden properties still serialized to JSON[/+] on Jun 6, 2019
  3. vexx32 commented on Jun 6, 2019

    @vexx32
    Collaborator

    /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 Convert cmdlets. 🙂

  4. Brad-Christie commented on Jun 6, 2019

    @Brad-Christie
    Author

    Looked quickly, appears XML uses a CustomSerialization serializer, which appears to iterate the PSObject.properties itself, which I assume implements some kind of filtering mechanism (through the PSMemberInfoIntegratingCollection<>). 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. 🤷‍♂️

  5. added
    WG-Engine
    on Jun 7, 2019
  6. thezim commented on Jun 11, 2019

    @thezim
    Contributor

    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.

  7. Brad-Christie commented on Jun 11, 2019

    @Brad-Christie
    Author

    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.

  8. mklement0 commented on Nov 17, 2019

    @mklement0
    Contributor

    Just to affirm that the current behavior is indeed unexpected:

    Select-Object (with wildcards) and ConvertTo-Csv / Export-Csv - sensibly - do not include hidden properties - ConvertTo-Json should act the same.

    (Export-CliXml does, but there it makes sense (as-faithful-as-possible serialization); the rarely used ConvertTo-Xml does not).

  9. iSazonov commented on Nov 26, 2019

    @iSazonov
    Collaborator

    May be fixed in #11198 as a side effect. :-)

  10. added
    Issue-Bug
    and removed
    Issue-Question
    on Jan 15, 2021
  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  14. added
    Resolution-No Activity
    on Nov 16, 2023
  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  16. nosalan commented on Nov 27, 2023

    @nosalan

    It is labeled as a bug, it should be reopened.

  17. removed
    Resolution-No Activity
    on Nov 29, 2023
  18. added
    Resolution-No Activity
    on May 28, 2024
  19. microsoft-github-policy-service commented on May 28, 2024

    @microsoft-github-policy-service
    Contributor

    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.

  20. dbramblett commented on Nov 15, 2024

    @dbramblett

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions