Skip to content

Commit

Permalink
Text.Json notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Jan 20, 2024
1 parent b10505a commit d15ca5f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ( ) {
Expand All @@ -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
Expand All @@ -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
Expand Down
46 changes: 46 additions & 0 deletions Pwsh/Modules/Text.Json/System.Text.Json.CheatSheet.md
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions Pwsh/Modules/Text.Json/TinyProc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -35,9 +35,9 @@ $Ps = get-process pwsh | Select -first 1
$testCoerce = [TinyProc]$Ps

$records = @(
[TinyProc]@{}
[TinyProc] @{}
[TinyProc]::new()
[TinyProc]$Ps
[TinyProc] $Ps
)
$records

Expand All @@ -48,5 +48,5 @@ variations you could also use:
@( get-process) -as [TinyProc[]]
| Out-Null

[TinyProc[]]@(get-process)
[TinyProc[]] @(get-process)
| Out-Null

0 comments on commit d15ca5f

Please sign in to comment.