I've an issue with my JSON. It works returns correctly in PHP 5.3 (so I can't use json_last_error()), and it returns successfully when I copy string explicitly into json_decode (json_decode('{...}'). It only returns null in when I pass the result as a variable and I'm using php 5.2, which is what I need it for.
The output comes from JSON logging in PHPUnit:
[
{
"event": "suiteStart",
"suite": "",
"tests": 2
},
{
"event": "suiteStart",
"suite": "TagTestCase",
"tests": 2
},
{
"event": "test",
"suite": "TagTestCase",
"test": "TagTestCase::test_it",
"status": "fail",
"time": 0.00248718261719,
"trace": [
{
"file": "\/UnitTest\/PHPUnit.php",
"line": 98,
"function": "run",
"class": "PHPUnit_Framework_TestSuite",
"type": "->",
"args": [
{
}
]
},
{
"file": "\/UnitTest\/PHPUnit.php",
"line": 116,
"function": "run",
"class": "PHPUnit",
"type": "->",
"args": [
]
},
{
"file": "\/UnitTest\/PHPUnit.php",
"line": 212,
"function": "__tostring",
"class": "PHPUnit",
"type": "->",
"args": [
]
}
],
"message": "false assertionzzzzz.\nFailed asserting that <boolean:false> is true."
},
{
"event": "test",
"suite": "TagTestCase",
"test": "TagTestCase::test_two",
"status": "pass",
"time": 0.00182914733887,
"trace": [
],
"message": ""
}
]
EDIT: These are the paths, I've been exploring - maybe you are a better explorer.. Three possible paths that could help:
Any help would be greatly(!) appreciated.
Thanks! Matt
What a HORRENDOUS debug session.. well there's good news.. I figured it out..
I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle "
, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:
$json = str_replace('"', '"', $json);
Now I thought they were the same.. maybe someone can enlighten me..
Since PHP 7.3, the json_decode function will accept a new JSON_THROW_ON_ERROR option that will let json_decode throw an exception instead of returning null on error.
Example:
try {
json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (\JsonException $exception) {
echo $exception->getMessage(); // displays "Syntax error"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With