Json.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.2.1
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. /**
  15. * Json
  16. *
  17. * Provides JSON functions in an object oriented way.
  18. */
  19. class Json
  20. {
  21. /**
  22. * Returns a string containing the JSON representation of the given input
  23. *
  24. * @access public
  25. * @static
  26. * @param mixed $input
  27. * @throws Exception
  28. * @return string
  29. */
  30. public static function encode($input)
  31. {
  32. $jsonString = json_encode($input);
  33. $errorCode = json_last_error();
  34. if ($errorCode === JSON_ERROR_NONE) {
  35. return $jsonString;
  36. }
  37. $message = 'A JSON error occurred';
  38. if (function_exists('json_last_error_msg')) {
  39. $message .= ': ' . json_last_error_msg();
  40. }
  41. $message .= ' (' . $errorCode . ')';
  42. throw new Exception($message, 90);
  43. }
  44. }