tags:

views:

77

answers:

3

Hey all,

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:

  • What is different about json_decode() in php 5.2 then 5.3? what did they change?
  • Someone else using JSON from PHPUnit, and how they parse it.
  • What changes when you have it in a variable vs. printing it to screen and copying it into json_decode()

Any help would be greatly(!) appreciated.

Thanks! Matt

A: 

try to set the error reporting in ALL, the json_decode() should give you a notice in the offset where the conversion fails.

shadow_of__soul
Nope.. doesn't do anything.. :-(
Matt
I added error_reporting(E_ALL).. that's what you suggested right??
Matt
yes that what i mean, i always used and worked for me before :s
shadow_of__soul
+2  A: 

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 &quot;, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('&quot;', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Matt
But I'm not seeing any `quot` s in the JSON you quote?
Pekka
Well... my json was taken from a print_r() statement which must have converted the symbols before I copied and pasted...
Matt
+2  A: 

When I use:

phpunit --log-json file.json <test_file>

(using PHPUnit 3.4.13), The file that it creates does not appear to contain valid JSON,

the json file contains "json" which looks something like:

{...}{...}{...}{...}

Instead of what I would expect to see:

[{...},{...},{...},{...}]

Not sure if the is the same problem that you're seeing, your sample JSON output in the question appears to be more valid that what I'm seeing.

Once adding the missing commas and brackets, it can be parsed with json_decode() on PHP 5.2.10 or PHP 5.3.2.

Keith Minkler
Yes, that had to be fixed too.. haha. I took care of that one though.
Matt
Oh and for whatever reason, just adding the commas and brackets didn't fix it (see my answer), not sure why though - maybe it was something I was doing in my code to convert `"` to `"`
Matt
Can you clarify on how you are getting the JSON from PHPUnit? I was just doing --log-json file.json, then in PHP, something like json_decode(file_get_contents("file.json")). Is this what you're doing as well? I didn't encounter the same problem as you with the quotes, but that might be because of the difference between our test cases.
Keith Minkler