2016-08-11 08:41:52 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2018-08-11 13:29:58 -04:00
|
|
|
* @version 1.2.1
|
2016-08-11 08:41:52 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:50:00 -05:00
|
|
|
namespace PrivateBin;
|
2016-08-11 08:41:52 -04:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Json
|
|
|
|
*
|
|
|
|
* Provides JSON functions in an object oriented way.
|
|
|
|
*/
|
|
|
|
class Json
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a string containing the JSON representation of the given input
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param mixed $input
|
|
|
|
* @throws Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function encode($input)
|
|
|
|
{
|
|
|
|
$jsonString = json_encode($input);
|
2016-08-15 10:45:47 -04:00
|
|
|
$errorCode = json_last_error();
|
2016-08-11 08:41:52 -04:00
|
|
|
if ($errorCode === JSON_ERROR_NONE) {
|
|
|
|
return $jsonString;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = 'A JSON error occurred';
|
|
|
|
if (function_exists('json_last_error_msg')) {
|
|
|
|
$message .= ': ' . json_last_error_msg();
|
|
|
|
}
|
|
|
|
$message .= ' (' . $errorCode . ')';
|
|
|
|
throw new Exception($message, 90);
|
|
|
|
}
|
|
|
|
}
|