PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Elastica/lib/Elastica/JSON.php

https://github.com/cceh/atom
PHP | 67 lines | 28 code | 10 blank | 29 comment | 4 complexity | faec79e5c7a9df777275339c7940a81a MD5 | raw file
Possible License(s): CC-BY-3.0, AGPL-3.0, MIT, ISC
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\JSONParseException;
  4. /**
  5. * Elastica JSON tools
  6. *
  7. * @package Elastica
  8. */
  9. class JSON
  10. {
  11. /**
  12. * Parse JSON string to an array
  13. *
  14. * @param string $json JSON string to parse
  15. * @return array PHP array representation of JSON string
  16. * @link http://php.net/manual/en/function.json-decode.php
  17. * @link http://www.php.net/manual/en/function.json-last-error.php
  18. */
  19. public static function parse(/* inherit from json_decode */)
  20. {
  21. // extract arguments
  22. $args = func_get_args();
  23. // default to decoding into an assoc array
  24. if (sizeof($args) === 1) {
  25. $args[] = true;
  26. }
  27. // run decode
  28. $array = call_user_func_array('json_decode', $args);
  29. // turn errors into exceptions for easier catching
  30. $error = json_last_error();
  31. if ($error !== JSON_ERROR_NONE) {
  32. throw new JSONParseException($error);
  33. }
  34. // output
  35. return $array;
  36. }
  37. /**
  38. * Convert input to JSON string with standard options
  39. *
  40. * @param mixed check args for PHP function json_encode
  41. * @return string Valid JSON representation of $input
  42. * @link http://php.net/manual/en/function.json-encode.php
  43. */
  44. public static function stringify(/* inherit from json_encode */)
  45. {
  46. // extract arguments
  47. $args = func_get_args();
  48. // allow special options value for Elasticsearch compatibility
  49. if (sizeof($args) > 1 && $args[1] === 'JSON_ELASTICSEARCH') {
  50. // Use built in JSON constants if available (php >= 5.4)
  51. $args[1] = (defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_SLASHES : 64)
  52. | (defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 256);
  53. }
  54. // run encode and output
  55. return call_user_func_array('json_encode', $args);
  56. }
  57. }