|
| 1 | +#Requires -Version 7 |
| 2 | +using namespace System.Collections.Generic |
| 3 | +using namespace System.Text |
| 4 | +using namespace System.Text.Json |
| 5 | +using namespace System.Text.Json.Serialization |
| 6 | +using namespace System.Linq |
| 7 | + |
| 8 | +$assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop' |
| 9 | + |
| 10 | +# This isn't an example of what you should do, it's testing what you can do |
| 11 | + |
| 12 | +function AutoJson { |
| 13 | + <# |
| 14 | + .SYNOPSIS |
| 15 | + Try one of the automatic methods |
| 16 | + .notes |
| 17 | + [System.Text.Json.JsonSerializer] has a ton of overloads |
| 18 | + .LINK |
| 19 | + https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer?view=net-8.0#methods |
| 20 | + #> |
| 21 | + [CmdletBinding()] |
| 22 | + param( |
| 23 | + [Parameter(ValueFromPipeline)] |
| 24 | + [Alias('InObj', 'In')] |
| 25 | + [object] $Object, |
| 26 | + |
| 27 | + # Without a type, it falls back to GetType() |
| 28 | + [Alias('Tinfo', 'T')] |
| 29 | + [type] $TypeInfo |
| 30 | + ) |
| 31 | + process { |
| 32 | + if(-not $TypeInfo ) { $TypeInfo = $Object.GetType() } |
| 33 | + [Text.Json.JsonSerializer]::Serialize( <# value: #> $Object, <# tinfo #> $TypeInfo ) |
| 34 | + |
| 35 | + 'AutoJson: TryConvert type: {0}' -f $TypeInfo | Write-verbose |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +$error.clear() |
| 40 | +$PSDefaultParameterValues['AutoJson:Ea'] = 'break' |
| 41 | +$PSDefaultParameterValues['AutoJson:Ea'] = 'continue' |
| 42 | + |
| 43 | +class SomePS { |
| 44 | + [string] $ProcessName |
| 45 | + [int] $Id |
| 46 | + |
| 47 | + |
| 48 | + # [example kind 1]: Property on instances to include, but, never serialize it |
| 49 | + [Serialization.JsonIgnoreAttribute()] |
| 50 | + [Diagnostics.ProcessModuleCollection] $Modules |
| 51 | + |
| 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 | + |
| 57 | + SomePS ( [object]$Other ) { |
| 58 | + $WantedProps = [Linq.Enumerable]::Intersect( # because linq is fun |
| 59 | + [string[]] $This.PSObject.Properties.Name, |
| 60 | + [string[]] $Other.PSObject.Properties.Name ) |
| 61 | + |
| 62 | + foreach($PropName in $WantedProps) { |
| 63 | + $This.$PropName = $Other.$PropName |
| 64 | + } |
| 65 | + # hard coded version: |
| 66 | + # $This.ProcessName = $Other.ProcessName |
| 67 | + # $This.Id = $Other.Id |
| 68 | + # $this.MainWindowTitle = $Other.MainWindowTitle |
| 69 | + # $This.Modules = $Other.Module |
| 70 | + } |
| 71 | +} |
| 72 | +$hasModules = get-process | ? Modules | select -First 1 |
| 73 | +$hasTitle = ( ps ) | ? MainWindowTitle | select -first 1 |
| 74 | +$noTitle = ( ps ) | ? -not MainWindowTitle | select -first 1 |
| 75 | +$notNullSite = (get-process | ?{ $Null -eq $_.site }) |
| 76 | + |
| 77 | +# [a] works |
| 78 | +$hasModules -as [SomePS[]] |
| 79 | +<# Out: |
| 80 | + ProcessName Id Modules |
| 81 | + ----------- -- ------- |
| 82 | + ApplicationFrameHost 19636 {System.Diagnostics.ProcessModule (ApplicationFrameHost.exe), System.Diagnost .... |
| 83 | +#> |
| 84 | + |
| 85 | +$hasModules -as [SomePS[]] | %{ AutoJson -InObj $_ -TypeInfo ([SomePS]) } |
| 86 | +<# Out: |
| 87 | + {"ProcessName":"ApplicationFrameHost","Id":19636} |
| 88 | +#> |
| 89 | + |
| 90 | +# cool, typedata is constrained to derived types |
| 91 | +{ |
| 92 | + $hasModules -as [SomePS[]] | %{ AutoJson -InObj $_ -TypeInfo ([System.Diagnostics.Process]) } |
| 93 | +} | Should -Throw -because 'Typedef used did not derive from type' |
| 94 | +<# Out: |
| 95 | + Exception calling "Serialize" with "2" argument(s): "The specified |
| 96 | + type [System.Diagnostics.Process] must derive from the specific value's type [SomePS]." |
| 97 | +#> |
| 98 | + |
| 99 | +# [b] not quite working |
| 100 | +h1 '[b]' |
| 101 | +$hasTitle -as [SomePs[]] | %{ AutoJson -InObj $_ } |
| 102 | + |
| 103 | + |
| 104 | +$hasModules, $hasTitle, $noTitle -as [SomePs[]] |
| 105 | + | AutoJson |
| 106 | + |
| 107 | +<# Outputs: |
| 108 | + {"ProcessName":"ApplicationFrameHost","Id":19636,"MainWindowTitle":"Snip \u0026 Sketch"} |
| 109 | + {"ProcessName":"AggregatorHost","Id":9268,"MainWindowTitle":""} |
| 110 | +#> |
0 commit comments