Skip to content

Commit

Permalink
json test
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Jan 29, 2024
1 parent 6fbb03c commit 3634226
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Pwsh/Modules/Text.Json/sketch/dynamicJsonClass.ps1
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)
#>

0 comments on commit 3634226

Please sign in to comment.