From d15ca5f468ff5f19661d5fee0b0ed895cc0b00a3 Mon Sep 17 00:00:00 2001 From: Jake Bolton Date: Sat, 20 Jan 2024 09:56:39 -0600 Subject: [PATCH] Text.Json notes --- ...ignoring properties - System.Text.Json.ps1 | 33 ++++++++----- .../Text.Json/System.Text.Json.CheatSheet.md | 46 +++++++++++++++++++ Pwsh/Modules/Text.Json/TinyProc.ps1 | 14 +++--- 3 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 Pwsh/Modules/Text.Json/System.Text.Json.CheatSheet.md 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 e0fd6cc..abf9170 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 @@ -1,5 +1,6 @@ -using namespace System.Text.Json -using namespace System.Collections.Generic +using namespace System.Collections.Generic +using namespace System.Text +using namespace System.Text.Json using namespace System.Text.Json.Serialization $assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop' goto $PSScriptRoot @@ -75,10 +76,16 @@ class SimpleProcess { [Datetime]$When # a property that normally errors out (with default serialization) - [System.Text.Json.Serialization.JsonIgnoreAttribute()] + # or: [System.Text.Json.Serialization.JsonIgnoreAttribute()] + [Serialization.JsonIgnoreAttribute()] [Diagnostics.ProcessModuleCollection]$Modules # and a property to only ignore when null + + # [Serialization.JsonIgnoreCondition( + # 'always' + # )] + [JsonIgnoreAttribute( Condition = [JsonIgnoreCondition]::WhenWritingNull) ] [object]$MaybeData SimpleProcess ( ) { @@ -96,21 +103,28 @@ class SimpleProcess { } } -[SimpleProcess]@(ps | s -first 1 ) +$pslist = [SimpleProcess[]] @( ps | s -first 2) +$psList[0].MaybeData = @{ Name = 'Jen' ; Species = 'cat' } + +h1 'should only serialize .MaybeData for one result' +AutoJson $pslist| jq return -[List[Object]]$Records = @( + +[SimpleProcess] @(ps | s -first 1 ) +# return +[List[Object]] $Records = @( # class coerce from an object - [SimpleProcess](get-process pwsh | s -First 1) + [SimpleProcess] (get-process pwsh | s -First 1) # explicit ctor [SimpleProcess]::new() # automatic if parameterless c-tor: - [SimpleProcess]@{} + [SimpleProcess] @{} # a similar variation $One = Get-Process 'pwsh' | Select -first 1 - [SimpleProcess]$One + [SimpleProcess] $One ) $records | ft -auto @@ -119,10 +133,7 @@ $records | ft -auto AutoJson $Records @' [{"Name":null,"CommandLine":"pwsh.exe -nol","When":"2024-01-19T15:13:20.8026851-06:00"},{"Name":null,"CommandLine":null,"When":"2024-01-19T15:13:21.0254829-06:00"},{"Name":null,"CommandLine":"","When":"2024-01-19T15:13:21.0266413-06:00"},{"Name":null,"CommandLine":"pwsh.exe -nol","When":"2024-01-19T15:13:21.0397003-06:00"}] -'@ - -@' If you didn't ignore that property, you'd get an error Error will occur if 'modules' isn't excluded diff --git a/Pwsh/Modules/Text.Json/System.Text.Json.CheatSheet.md b/Pwsh/Modules/Text.Json/System.Text.Json.CheatSheet.md new file mode 100644 index 0000000..b8ed641 --- /dev/null +++ b/Pwsh/Modules/Text.Json/System.Text.Json.CheatSheet.md @@ -0,0 +1,46 @@ +- [To Import Namespace](#to-import-namespace) +- [Serialization](#serialization) + - [Ignoring Properties : `JsonIgnoreAttribute`](#ignoring-properties--jsonignoreattribute) + - [Ignore When null : `JsonIgnoreCondition`](#ignore-when-null--jsonignorecondition) + - [Always Ignore](#always-ignore) +- [Deserialization](#deserialization) + +## To Import Namespace + +```ps1 +using namespace System.Collections.Generic +using namespace System.Text +using namespace System.Text.Json +using namespace System.Text.Json.Serialization +$assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop' +``` + +## Serialization + +### Ignoring Properties : `JsonIgnoreAttribute` + +#### Ignore When null : `JsonIgnoreCondition` + +```ps1 +class SimpleProcess { + [string] $Name + + [JsonIgnoreAttribute( Condition = [JsonIgnoreCondition]::WhenWritingNull) ] + [object]$MaybeData +} +``` + +#### Always Ignore + +```ps1 +class SimpleProcess { + [string] $Name + + # always skip + [Serialization.JsonIgnoreAttribute()] + [Diagnostics.ProcessModuleCollection] $Modules +} +``` + + +## Deserialization diff --git a/Pwsh/Modules/Text.Json/TinyProc.ps1 b/Pwsh/Modules/Text.Json/TinyProc.ps1 index f6b4efa..0d177d3 100644 --- a/Pwsh/Modules/Text.Json/TinyProc.ps1 +++ b/Pwsh/Modules/Text.Json/TinyProc.ps1 @@ -12,16 +12,16 @@ At least the Linq part #> class TinyProc { - [string]$Name - [datetime]$When + [string] $Name + [datetime] $When TinyProc () { $this.When = Get-date } TinyProc ( [object]$Other ) { $WantedProps = [Linq.Enumerable]::Intersect( - [string[]]$This.PSObject.Properties.Name, - [string[]]$Other.PSObject.Properties.Name ) + [string[]] $This.PSObject.Properties.Name, + [string[]] $Other.PSObject.Properties.Name ) $This.When = Get-Date @@ -35,9 +35,9 @@ $Ps = get-process pwsh | Select -first 1 $testCoerce = [TinyProc]$Ps $records = @( - [TinyProc]@{} + [TinyProc] @{} [TinyProc]::new() - [TinyProc]$Ps + [TinyProc] $Ps ) $records @@ -48,5 +48,5 @@ variations you could also use: @( get-process) -as [TinyProc[]] | Out-Null -[TinyProc[]]@(get-process) +[TinyProc[]] @(get-process) | Out-Null