I'm doing an application that uses json. For testeability purposes I made a JsonWrapper:
<?php
class JsonWrapper {
private $json_errors;
public function __construct() {
$this->json_errors = array(
JSON_ERROR_DEPTH => ' - Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => ' - Underflow or the modes mismatch',
JSON_ERROR_CTRL_CHAR => ' - Unexpected control character found',
JSON_ERROR_SYNTAX => ' - Syntax error, malformed JSON',
JSON_ERROR_UTF8 => ' - Malformed UTF-8 characters, possibly incorrectly encoded'
);
}
public function decode($json, $toAssoc = false) {
$result = json_decode($json, $toAssoc);
$errorIndex = json_last_error();
if (isset($this->json_errors[$errorIndex])) {
throw new RuntimeException('JSON Error: ' . $this->json_errors[$errorIndex]);
}
return $result;
}
}
As you see, I map all the json error constants with my own error message.
The problem:
- If I don't use HipHop this works fine.
- If I use HipHop I get this error:
Use of undefined constant JSON_ERROR_DEPTH - assumed 'JSON_ERROR_DEPTH'
I tried to modify the code commenting all the initialization of $json_errors
, and test if the error was because the associative array is forbidden in HipHop, but it wasn't that. It kept failing after I put a json constant anywhere on my code.
I also did a test if all the php constants were failing. I test with the 'XML_ERROR_PARTIAL_CHAR' constant, and It didn't fail!
I really don't know what is happening in here and why HipHop hates JSON so much :(
Edit
The main question is:
Why hiphop doesn't understand JSON_ERROR_DEPTH but resolves with no much effort XML_ERROR_PARTIAL_CHAR, if both are defined the same way with the define php function?
json.php: line 170
/**
* The maximum stack depth has been exceeded.
* Available since PHP 5.3.0.
* @link http://php.net/manual/en/json.constants.php
*/
define ('JSON_ERROR_DEPTH', 1);
xml.php: line 559
define ('XML_ERROR_PARTIAL_CHAR', 6);