< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Write-KrJsonResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrJsonResponse.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 72
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 0% (0/3) Total lines: 67 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 18:11:31 Line coverage: 0% (0/3) Total lines: 68 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/12/2025 - 03:43:11 Line coverage: 0% (0/4) Total lines: 71 Tag: Kestrun/Kestrun@d160286e3020330b1eb862d66a37db2e26fc904209/16/2025 - 16:28:42 Line coverage: 0% (0/8) Total lines: 72 Tag: Kestrun/Kestrun@d5c0d6132e97ca542441289c02a4c9e9d0364d49

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrJsonResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Writes an object to the HTTP response body as JSON.
 4    .DESCRIPTION
 5        Serializes the provided object to JSON using Newtonsoft.Json and writes it
 6        to the current HTTP response. The caller can specify the HTTP status code,
 7        serialization depth and formatting options.
 8    .PARAMETER InputObject
 9        The object to serialize and write to the response.
 10    .PARAMETER StatusCode
 11        The HTTP status code to set for the response.
 12    .PARAMETER Depth
 13        The maximum depth of the JSON serialization.
 14    .PARAMETER Compress
 15        Whether to compress the JSON output.
 16    .PARAMETER ContentType
 17        The content type of the response.
 18    .EXAMPLE
 19        PS> $myObject | Write-KrJsonResponse -StatusCode 201 -Depth 5 -Compress -ContentType "application/json"
 20        Serializes the object to JSON and writes it to the response with the specified options.
 21    .EXAMPLE
 22        PS> $myObject | Write-KrJsonResponse -StatusCode 400 -Depth 3 -Compress -ContentType "application/json"
 23        Serializes the object to JSON and writes it to the response with the specified options.
 24    .EXAMPLE
 25        PS> $myObject | Write-KrJsonResponse -StatusCode 500 -Depth 2
 26        Serializes the object to JSON and writes it to the response with the specified options.
 27#>
 28function Write-KrJsonResponse {
 29    [KestrunRuntimeApi('Route')]
 30    [CmdletBinding()]
 31    param(
 32        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
 33        [object]$InputObject,
 34
 35        [Parameter()]
 36        [int]$StatusCode = 200,
 37
 38        [Parameter()]
 39        [ValidateRange(0, 100)]
 40        [int]$Depth = 10,
 41
 42        [Parameter()]
 43        [switch]$Compress,
 44
 45        [Parameter()]
 46        [string]$ContentType
 47    )
 48    begin {
 49        # Collect all piped items
 050        $items = [System.Collections.Generic.List[object]]::new()
 051        $ContentType = [string]::IsNullOrEmpty($ContentType) ? 'application/json' : $ContentType
 52    }
 53    process {
 54        # Accumulate; no output yet
 055        $items.Add($InputObject)
 56    }
 57    end {
 58        # Only works inside a route script block where $Context is available
 059        if ($null -eq $Context -or $null -eq $Context.Response) {
 060            Write-KrOutsideRouteWarning
 61            return
 62        }
 63        #  - single item by default when only one was piped
 64        #  - array if multiple items were piped
 065        $payload = if ($items.Count -eq 1) { $items[0] } else { $items.ToArray() }
 66
 067        $json = ConvertTo-Json -InputObject $payload -Depth $Depth -Compress:$Compress
 68        # Write the JSON response
 069        $Context.Response.WriteTextResponse($json, $StatusCode, $ContentType)
 70    }
 71}
 72

Methods/Properties

Write-KrJsonResponse()