From b10505a2f1aaa9ee24f4c6c15e74f2f176dbe2f1 Mon Sep 17 00:00:00 2001 From: Jake Bolton Date: Fri, 19 Jan 2024 17:27:22 -0600 Subject: [PATCH] Json Serialization Demo --- ... ignoring properties - System.Text.Json.ps1 | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Pwsh/Modules/Text.Json/Attributes for conditionally ignoring properties - System.Text.Json.ps1 b/Pwsh/Modules/Text.Json/Attributes for conditionally ignoring properties - System.Text.Json.ps1 index e177fc0..e0fd6cc 100644 --- a/Pwsh/Modules/Text.Json/Attributes for conditionally ignoring properties - System.Text.Json.ps1 +++ b/Pwsh/Modules/Text.Json/Attributes for conditionally ignoring properties - System.Text.Json.ps1 @@ -33,9 +33,9 @@ As a base case, automatically serialize all properties (if able to) '@ class Forecast { - [datetime]$Date - [int]$Degrees - [string]$Summary + [datetime] $Date + [int] $Degrees + [string] $Summary } [Forecast]$CastObj = @{ @@ -74,16 +74,20 @@ class SimpleProcess { [Datetime]$When - # a property that normally errors out: + # a property that normally errors out (with default serialization) [System.Text.Json.Serialization.JsonIgnoreAttribute()] [Diagnostics.ProcessModuleCollection]$Modules - # $Modules = 0..10 + + # and a property to only ignore when null + [object]$MaybeData SimpleProcess ( ) { - $This.When = Get-Date + $This.When = Get-Date + $this.MaybeData = $Null } SimpleProcess ( [object]$Other ) { $this.When = Get-Date + $this.MaybeData = $Null $This.CommandLine = ($Other)?.CommandLine ?? '' $This.Modules = ($Other)?.Modules # $this.Modules = ($Other)?.Modules @@ -92,6 +96,8 @@ class SimpleProcess { } } +[SimpleProcess]@(ps | s -first 1 ) +return [List[Object]]$Records = @( # class coerce from an object [SimpleProcess](get-process pwsh | s -First 1)