From 2952f6a3b6a48e7d1218d9d4049510f36f05f1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 31 Aug 2017 11:59:58 +0100 Subject: [PATCH] Added \JsonSerializable interface to Response->json (issue: #284). --- src/Pecee/Http/Response.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index c60743d..2fed825 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -69,13 +69,19 @@ class Response { } /** - * Json encode array - * @param array $value + * Json encode + * @param array|\JsonSerializable $value + * @throws \InvalidArgumentException; */ - public function json(array $value) { + public function json($value) { + + if(($value instanceof \JsonSerializable) === false && is_array($value) === false) { + throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.'); + } + $this->header('Content-type: application/json'); echo json_encode($value); - die(); + exit(0); } /**