function drush_json_encode

8.0.x output.inc drush_json_encode($var)
6.x output.inc drush_json_encode($var)
7.x output.inc drush_json_encode($var)
4.x drush.inc drush_json_encode($var)
5.x output.inc drush_json_encode($var)
master output.inc drush_json_encode($var)

Converts a PHP variable into its Javascript equivalent.

We provide a copy of D7's drupal_json_encode since this function is unavailable on earlier versions of Drupal.

See also

drupal_json_decode()

Related topics

2 calls to drush_json_encode()
drush_outputformat_json::format in commands/core/outputformat/json.inc
JSONCache::writeFile in lib/Drush/Cache/JSONCache.php
Serializes data and write it to the given filename.

File

includes/output.inc, line 735

Code

function drush_json_encode($var) {
  if (version_compare(phpversion(), '5.4.0', '>=')) {
    $json = json_encode($var, JSON_PRETTY_PRINT);
  }
  else {
    $json = json_encode($var);
  }
  // json_encode() does not escape <, > and &, so we do it with str_replace().
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), $json);
}