2016-08-11 20:41:52 +08: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
|
2021-04-05 22:44:12 +08:00
|
|
|
* @version 1.3.5
|
2016-08-11 20:41:52 +08:00
|
|
|
*/
|
2016-12-13 01:43:23 +08:00
|
|
|
|
2016-12-13 01:50:00 +08:00
|
|
|
namespace PrivateBin;
|
2016-08-11 20:41:52 +08: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);
|
2019-05-14 04:31:52 +08:00
|
|
|
self::_detectError();
|
|
|
|
return $jsonString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with the contents as described in the given JSON input
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $input
|
|
|
|
* @throws Exception
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function decode($input)
|
|
|
|
{
|
|
|
|
$array = json_decode($input, true);
|
|
|
|
self::_detectError();
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detects JSON errors and raises an exception if one is found
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @throws Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function _detectError()
|
|
|
|
{
|
2019-05-19 15:42:55 +08:00
|
|
|
$errorCode = json_last_error();
|
2016-08-11 20:41:52 +08:00
|
|
|
if ($errorCode === JSON_ERROR_NONE) {
|
2019-05-14 04:31:52 +08:00
|
|
|
return;
|
2016-08-11 20:41:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$message = 'A JSON error occurred';
|
|
|
|
if (function_exists('json_last_error_msg')) {
|
|
|
|
$message .= ': ' . json_last_error_msg();
|
|
|
|
}
|
|
|
|
$message .= ' (' . $errorCode . ')';
|
|
|
|
throw new Exception($message, 90);
|
|
|
|
}
|
|
|
|
}
|