Converts a PHP variable into its JavaScript equivalent.
Paramètres
mixed $data: Usually an array of data to be converted into a JSON string.
Return value
string If there are no errors, this will return a JSON string. FALSE will be returned on failure.
2 calls to advagg_json_encode()
- advagg_pre_render_scripts dans ./
advagg.module - Callback for pre_render to add elements needed for JavaScript to be rendered.
- _advagg_process_html dans ./
advagg.module - Replacement for template_process_html().
Fichier
-
./
advagg.module, line 4754
Code
function advagg_json_encode($data) {
// Different versions of PHP handle json_encode() differently.
static $php550;
static $php540;
static $php530;
if (!isset($php550)) {
$php550 = version_compare(PHP_VERSION, '5.5.0', '>=');
}
if (!isset($php540)) {
$php540 = version_compare(PHP_VERSION, '5.4.0', '>=');
}
if (!isset($php530)) {
$php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
}
// Use fallback drupal encoder if PHP < 5.3.0.
if (!$php530) {
return @drupal_json_encode($data);
}
// Default json encode options.
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
if ($php550 && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 0) {
// Output partial json if not in development mode and PHP >= 5.5.0.
$options |= JSON_PARTIAL_OUTPUT_ON_ERROR;
}
if ($php540 && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
// Pretty print JSON if in development mode and PHP >= 5.4.0.
$options |= JSON_PRETTY_PRINT;
}
// Encode to JSON.
$json_data = @json_encode($data, $options);
// Uses json_last_error() if in development mode.
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
$error_number = json_last_error();
switch ($error_number) {
case JSON_ERROR_NONE:
$error_message = '';
break;
case JSON_ERROR_DEPTH:
$error_message = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error_message = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error_message = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error_message = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error_message = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error_message = 'Unknown error: ' . $error_number;
break;
}
if (!empty($error_message)) {
if (is_callable('httprl_pr')) {
$pretty_data = httprl_pr($data);
}
elseif (is_callable('kprint_r')) {
// @codingStandardsIgnoreLine
$pretty_data = kprint_r($data, TRUE);
}
else {
$pretty_data = '<pre>' . filter_xss(print_r($data, TRUE)) . '</pre>';
}
watchdog('advagg_json', 'Error with json encoding the Drupal.settings value. Error Message: %error_message. JSON Data: !data', array(
'%error_message' => $error_message,
'!data' => $pretty_data,
), WATCHDOG_ERROR);
}
}
return $json_data;
}