-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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' | ||
pushd $PSScriptRoot | ||
function AutoJson { | ||
<# | ||
.SYNOPSIS | ||
Try one of the automatic methods | ||
.notes | ||
[System.Text.Json.JsonSerializer] has a ton of overloads | ||
.LINK | ||
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer?view=net-8.0#methods | ||
#> | ||
param( | ||
[Alias('InObj', 'In')] | ||
[object] $Object, | ||
|
||
# Without a type, it falls back to GetType() | ||
[Alias('Tinfo', 'T')] | ||
[type] $TypeInfo | ||
) | ||
if(-not $TypeInfo ) { $TypeInfo = $Object.GetType() } | ||
[JsonSerializer]::Serialize( <# value: #> $Object, <# tinfo #> $TypeInfo ) | ||
} | ||
# [Serialization.JsonIgnore(Condition = [Serialization.JsonIgnoreCondition]::Always)] | ||
|
||
# [JsonSerializer]::Serialize( <# value: #> $CastObj, <# jsonTypeInfo: #> [Forecast]) | ||
# [JsonSerializer]::Serialize( <# value: #> $CastObj, $CastObj.GetType() ) | ||
|
||
class SimpleProcess { | ||
[string]$Name | ||
[string]$CommandLine | ||
|
||
[Serialization.JsonIgnoreAttribute()] | ||
[Diagnostics.ProcessModuleCollection] $Modules | ||
|
||
SimpleProcess ( ) { | ||
} | ||
SimpleProcess ( [object]$Other ) { | ||
$This.Name = ($Other)?.Name ?? '' | ||
$This.CommandLine = ($Other)?.CommandLine ?? '' | ||
$This.Modules = ($Other)?.Modules | ||
} | ||
} | ||
|
||
$pslist = [SimpleProcess[]] @( Get-Process | Select-First -first 3) | ||
h1 'should only serialize .MaybeData for one result' | ||
AutoJson $pslist| jq | ||
|
||
<# | ||
other ways to instantiate object | ||
#> | ||
[SimpleProcess] @(get-process | select-object -first 1 ) | ||
# return | ||
[List[Object]] $Records = @( | ||
# class coerce from an object | ||
[SimpleProcess] (get-process pwsh | Select-Object -First 1) | ||
|
||
# explicit ctor | ||
[SimpleProcess]::new() | ||
|
||
# automatic if parameterless c-tor: | ||
[SimpleProcess] @{} | ||
|
||
# a similar variation | ||
$One = Get-Process 'pwsh' | Select -first 1 | ||
[SimpleProcess] $One | ||
) | ||
|
||
$records | ft -auto | ||
|
||
# neat, it works without extra work | ||
AutoJson $Records | ||
|
||
<# | ||
example output: | ||
[{"Name":"pwsh","CommandLine":"\u0022C:\\Program Files\\PowerShell\\7\\pwsh.exe\u0022 -WorkingDirectory ~"},{"Name":null,"CommandLine":null},{"Name":"","CommandLine":""},{"Name":"pwsh","CommandLine":"\u0022C:\\Program Files\\PowerShell\\7\\pwsh.exe\u0022 -WorkingDirectory ~"}] | ||
- [c# Text.Json intro](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to) | ||
#> |