| | 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 | | [bool]$Compress = $false, |
| | 44 | |
|
| | 45 | | [Parameter()] |
| | 46 | | [string]$ContentType |
| | 47 | | ) |
| | 48 | | process { |
| 0 | 49 | | if ($null -ne $Context.Response) { |
| 0 | 50 | | $ContentType = [string]::IsNullOrEmpty($ContentType) ? 'application/json' : $ContentType |
| 0 | 51 | | $Context.Response.WriteTextResponse((ConvertTo-Json -InputObject $InputObject -Depth $Depth -Compress:$Compr |
| | 52 | |
|
| | 53 | | <# To use the C# method directly, uncomment the following lines: |
| | 54 | | # Create a new JsonSerializerSettings object with the specified options |
| | 55 | | $serializerSettings = [Newtonsoft.Json.JsonSerializerSettings]::new() |
| | 56 | | $serializerSettings.Formatting = if ($Compress) { [Newtonsoft.Json.Formatting]::None } else { [Newtonsoft.Json.F |
| | 57 | | $serializerSettings.ContractResolver = [Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver]::n |
| | 58 | | $serializerSettings.ReferenceLoopHandling = [Newtonsoft.Json.ReferenceLoopHandling]::Ignore |
| | 59 | | $serializerSettings.NullValueHandling = [Newtonsoft.Json.NullValueHandling]::Ignore |
| | 60 | | $serializerSettings.DefaultValueHandling = [Newtonsoft.Json.DefaultValueHandling]::Ignore |
| | 61 | | $serializerSettings.MaxDepth = $Depth |
| | 62 | | $serializerSettings.DateFormatHandling = [Newtonsoft.Json.DateFormatHandling]::IsoDateFormat |
| | 63 | | # Call the C# method on the $Context.Response object |
| | 64 | | $Context.Response.WriteJsonResponse($InputObject, $serializerSettings, $StatusCode, $ContentType)#> |
| | 65 | | } |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|