diff options
author | Christopher Ng <chrng8@gmail.com> | 2024-07-30 18:19:55 -0700 |
---|---|---|
committer | Christopher Ng <chrng8@gmail.com> | 2024-08-01 09:14:44 -0700 |
commit | 8bbd3261434e4d0cc6eb6a4350e101a0428a4804 (patch) | |
tree | 63d469d851dfa0bdd93e0c8a9262970b983f1b02 /lib | |
parent | b859260423305620a5b2b15700cae417cc58affa (diff) | |
download | nextcloud-server-8bbd3261434e4d0cc6eb6a4350e101a0428a4804.tar.gz nextcloud-server-8bbd3261434e4d0cc6eb6a4350e101a0428a4804.zip |
feat: Allow passing additional encode flags for json response
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/public/AppFramework/Http/JSONResponse.php | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/public/AppFramework/Http/JSONResponse.php b/lib/public/AppFramework/Http/JSONResponse.php index 4890d483cba..7551d264e73 100644 --- a/lib/public/AppFramework/Http/JSONResponse.php +++ b/lib/public/AppFramework/Http/JSONResponse.php @@ -23,6 +23,11 @@ class JSONResponse extends Response { * @var T */ protected $data; + /** + * Additional `json_encode` flags + * @var int + */ + protected $encodeFlags; /** @@ -30,12 +35,20 @@ class JSONResponse extends Response { * @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'); } @@ -47,7 +60,7 @@ class JSONResponse extends Response { * @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); } /** |