Types/OpenPackage.Part/WriteJson.ps1

<#
.SYNOPSIS
    Writes Part Content as Json
.DESCRIPTION
    Writes Open Package Part Content as Json
#>

[Reflection.AssemblyMetadata('FilePattern', '\.jsonc?$')]
[Reflection.AssemblyMetadata('ContentTypePattern', '[/\+]jsonc?$')]
param(
# The object to write.
[Alias('Input','Content','Text')]
[PSObject]
$InputObject,

<#

Any options used to write the object

Supported Options:

|Option|Description|
|-|-|
|Depth|The serialization depth|
|Encoding|The text encoding|
|Stream|Optional destination stream|

#>

[Collections.IDictionary]
$Option = [Ordered]@{}
)

# If this object does not have a write text method, return.
if (-not $this.WriteText) { throw 'No `.WriteText()`'; return }

# If no depth was set,
if (-not $option.Depth) {
    # use double the format enumeration limit (by default, 8)
    $option.Depth = $FormatEnumerationLimit * 2
}

# If we have a .Package and .PartUri property
if ($InputObject.Package -is [IO.Packaging.Package] -and 
    $InputObject.PartUri -is [uri]) {
    # avoid putting them in the object
    $text = ConvertTo-Json -InputObject (
        $InputObject | 
            Select-Object -Property * -ExcludeProperty 'Package', 'PartUri'
    ) -Depth $Option.Depth
} else {
    # Convert any other objects to json.
    $text = ConvertTo-Json -InputObject $InputObject -Depth $Option.Depth
}

# Then, write the text
$this.WriteText($text, $Option)