Skip to content
Prev Previous commit
Next Next commit
feat: Allow passing additional encode flags for json response
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Aug 1, 2024
commit 8bbd3261434e4d0cc6eb6a4350e101a0428a4804
17 changes: 15 additions & 2 deletions lib/public/AppFramework/Http/JSONResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ class JSONResponse extends Response {
* @var T
*/
protected $data;
/**
* Additional `json_encode` flags
* @var int
*/
protected $encodeFlags;


/**
* constructor of JSONResponse
* @param T $data the object or array that should be transformed
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers
* @param int $encodeFlags Additional `json_encode` flags
* @since 6.0.0
* @since 30.0.0 Added `$encodeFlags` param
*/
public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
public function __construct(
mixed $data = [],
int $statusCode = Http::STATUS_OK,
array $headers = [],
int $encodeFlags = 0,
) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->encodeFlags = $encodeFlags;
$this->addHeader('Content-Type', 'application/json; charset=utf-8');
}

Expand All @@ -47,7 +60,7 @@ public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK,
* @throws \Exception If data could not get encoded
*/
public function render() {
return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR, 2048);
return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR | $this->encodeFlags, 2048);
}

/**
Expand Down