diff --git a/src/JWT.php b/src/JWT.php index bf064e34..67514b29 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -355,7 +355,7 @@ public static function jsonDecode(string $input) public static function jsonEncode(array $input): string { if (PHP_VERSION_ID >= 50400) { - $json = \json_encode($input, \JSON_UNESCAPED_SLASHES); + $json = \json_encode($input, \JSON_UNESCAPED_SLASHES|\JSON_FORCE_OBJECT); } else { // PHP 5.3 only $json = \json_encode($input); diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 8b23ad6b..e1984b34 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -321,6 +321,15 @@ public function testRSEncodeDecodeWithPassphrase() $this->assertEquals($decoded, $expected); } + public function testDecodesEmptyArrayAsObject() + { + $key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk'; + $payload = []; + $jwt = JWT::encode($payload, $key, 'HS256'); + $decoded = JWT::decode($jwt, new Key($key, 'HS256')); + $this->assertEquals((object) $payload, $decoded); + } + /** * @runInSeparateProcess * @dataProvider provideEncodeDecode