| | | 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 | | #> |
| | | 28 | | function 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 |
| | 0 | 50 | | $items = [System.Collections.Generic.List[object]]::new() |
| | 0 | 51 | | $ContentType = [string]::IsNullOrEmpty($ContentType) ? 'application/json' : $ContentType |
| | | 52 | | } |
| | | 53 | | process { |
| | | 54 | | # Accumulate; no output yet |
| | 0 | 55 | | $items.Add($InputObject) |
| | | 56 | | } |
| | | 57 | | end { |
| | | 58 | | # Only works inside a route script block where $Context is available |
| | 0 | 59 | | if ($null -eq $Context -or $null -eq $Context.Response) { |
| | 0 | 60 | | Write-KrOutsideRouteWarning |
| | | 61 | | return |
| | | 62 | | } |
| | | 63 | | # - single item by default when only one was piped |
| | | 64 | | # - array if multiple items were piped |
| | 0 | 65 | | $payload = if ($items.Count -eq 1) { $items[0] } else { $items.ToArray() } |
| | | 66 | | |
| | 0 | 67 | | $json = ConvertTo-Json -InputObject $payload -Depth $Depth -Compress:$Compress |
| | | 68 | | # Write the JSON response |
| | 0 | 69 | | $Context.Response.WriteTextResponse($json, $StatusCode, $ContentType) |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |