Skip to content

Commit

Permalink
Don't raise an error when json_decode() returns null and no error is …
Browse files Browse the repository at this point in the history
…reported
  • Loading branch information
mbabker committed May 26, 2018
1 parent 42258a6 commit 9b7fe12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Tests/format/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,16 @@ public function testDataEqualityInConvertedObjects()

$this->assertEquals($input, $output, 'Input and output data must be equal.');
}

/**
* @testdox Validate an Exception is not thrown when a string is decoded to null and no error is reported
*
* @covers Joomla\Registry\Format\Json::stringToObject
*/
public function testExceptionNotThrownWhenDecodedToNullWithoutError()
{
$class = new Json;

$this->assertInstanceOf('stdClass', $class->stringToObject('{}'));
}
}
2 changes: 1 addition & 1 deletion src/Format/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function stringToObject($data, array $options = array('processSections' =
$decoded = json_decode($data);

// Check for an error decoding the data
if ($decoded === null)
if ($decoded === null && json_last_error() !== JSON_ERROR_NONE)
{
throw new \RuntimeException(sprintf('Error decoding JSON data: %s', json_last_error_msg()));
}
Expand Down

0 comments on commit 9b7fe12

Please sign in to comment.