1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 08:14:06 +02:00

Fix isJSON

Thanks to Drainedsoul
This commit is contained in:
Daniel St. Jules
2014-02-13 06:32:54 -05:00
parent b990df3c61
commit 9399f7a694
2 changed files with 5 additions and 7 deletions

View File

@@ -1199,11 +1199,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/
public function isJson()
{
if (!$this->endsWith('}') && !$this->endsWith(']')) {
return false;
}
json_decode($this->str);
return !is_null(json_decode($this->str));
return (json_last_error() === JSON_ERROR_NONE);
}

View File

@@ -730,14 +730,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
public function isJsonProvider()
{
return array(
array(false, ''),
array(false, '123'),
array(true, ''),
array(true, '123'),
array(true, '{"foo": "bar"}'),
array(false, '{"foo":"bar",}'),
array(false, '{"foo"}'),
array(true, '["foo"]'),
array(false, '{"foo": "bar"]'),
array(false, '123', 'UTF-8'),
array(true, '123', 'UTF-8'),
array(true, '{"fòô": "bàř"}', 'UTF-8'),
array(false, '{"fòô":"bàř",}', 'UTF-8'),
array(false, '{"fòô"}', 'UTF-8'),