Skip to content

Commit e2efa6f

Browse files
authored
Update dynamicJsonClassAttributes-iter3.ps1
1 parent e0d82bd commit e2efa6f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Pwsh/Modules/Text.Json/sketch/dynamicJsonClassAttributes-iter3.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ class SomePS {
4444
[string] $ProcessName
4545
[int] $Id
4646

47-
# [example kind 2]: Serialize property if it's not null, but ignore it if it's null
48-
[JsonIgnoreAttribute( Condition = [JsonIgnoreCondition]::WhenWritingNull) ]
49-
[string] $MainWindowTitle # next: test if ignore condition works on empty strings
5047

5148
# [example kind 1]: Property on instances to include, but, never serialize it
5249
[Serialization.JsonIgnoreAttribute()]
5350
[Diagnostics.ProcessModuleCollection] $Modules
5451

52+
# [example kind 2]: Serialize property if it's not null, but ignore it if it's null
53+
# note: It does not work here beacuse [object][string]::empty is not null
54+
[JsonIgnoreAttribute( Condition = [JsonIgnoreCondition]::WhenWritingNull) ]
55+
[object] $MainWindowTitle
56+
5557
SomePS ( [object]$Other ) {
5658
$WantedProps = [Linq.Enumerable]::Intersect( # because linq is fun
5759
[string[]] $This.PSObject.Properties.Name,
@@ -70,6 +72,7 @@ class SomePS {
7072
$hasModules = get-process | ? Modules | select -First 1
7173
$hasTitle = ( ps ) | ? MainWindowTitle | select -first 1
7274
$noTitle = ( ps ) | ? -not MainWindowTitle | select -first 1
75+
$notNullSite = (get-process | ?{ $Null -eq $_.site })
7376

7477
# [a] works
7578
$hasModules -as [SomePS[]]

0 commit comments

Comments
 (0)