Skip to content

Class hidden properties still serialized to JSON #9847

@Brad-Christie

Description

@Brad-Christie

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.

Metadata

Metadata

Assignees

No one assigned

    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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions