Skip to content

Commit e67638d

Browse files
authored
fix: add flag to force object (#416)
1 parent a57a898 commit e67638d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/JWT.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public static function jsonDecode(string $input)
355355
public static function jsonEncode(array $input): string
356356
{
357357
if (PHP_VERSION_ID >= 50400) {
358-
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
358+
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES|\JSON_FORCE_OBJECT);
359359
} else {
360360
// PHP 5.3 only
361361
$json = \json_encode($input);

tests/JWTTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ public function testRSEncodeDecodeWithPassphrase()
321321
$this->assertEquals($decoded, $expected);
322322
}
323323

324+
public function testDecodesEmptyArrayAsObject()
325+
{
326+
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
327+
$payload = [];
328+
$jwt = JWT::encode($payload, $key, 'HS256');
329+
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
330+
$this->assertEquals((object) $payload, $decoded);
331+
}
332+
324333
/**
325334
* @runInSeparateProcess
326335
* @dataProvider provideEncodeDecode

0 commit comments

Comments
 (0)