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.
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);
HipHop
doesn't define all the same constants that PHP does. I mean, hiphop is like a re-implementation of PHP, where some things got lost, and other changed.
For example, the PDO constants like PDO::PARAM_INT
in HipHop are defined the old way, ie PDO_PARAM_INT
.
Also, if I remember correctly, the hiphop implementation for urlencode
was like php's raw_urlencode
(ie. encoding spaces as %20
as raw_urlencode
does in PHP, instead of encoding them to +
as PHP's urlencode
).
Also HipHop is not implementing a exactly PHP 5.2, because there is some support for namespaces (that came out in PHP 5.3), but they are buggy and not recommended to be use.
In short, you may have to patch some things in your code. For example, you could add to your bootstrap code something like:
<?php
if (!is_defined('JSON_ERROR_NONE')) define('JSON_ERROR_NONE', 0);
// same thing with other JSON_* constants