maison_alexandra/www/system-brs-core/library/cJSON/cJSON.php

178 lines
5.7 KiB
PHP

<?php
namespace BRSystems\Core\Library\cJSON;
use Exception;
/**
* JSON class,
* simple layer to use core function (PHP 5.2), PECL extension (PHP >= 4.3.0) or Pear ServiceJSON
* ______________________________________________________________
* @example
* echo cJSON::encode(1.23); // string, 1.23
* echo cJSON::decode("1.23"); // float, 1.23
* --------------------------------------------------------------
* @Compatibility PHP 5.2 / PHP with json PECL extension / PHP with Pear ServiceJSON
* @Dependencies Pear ServiceJSON (file JSON.php) [http://pear.php.net/pepr/pepr-proposal-show.php?id=198]
* if there isn't Pecl extension [http://pecl.php.net/package/json]
* or PHP version is less than 5.2
* @Author Andrea Giammarchi
* @Site http://www.devpro.it/
* @Date 2006/11/06
* @LastMOd 2006/11/19 [faster conversion problems (Pear and Pecl) in range 00-1f]
* @Version 0.3b
*/
class cJSON
{
/**
* public "static" method,
* converts a JSON string into compatible value.
* cJSON::decode($input:String[, $assoc:Bool]):Mixed
*
* @param String $input JSON compatible string
* @param Bool $assoc true to convert object into associative arrays false by default
* @return Mixed Array, Standard Class, String, Float, integer, Boolean or null value.
*/
public static function decode($input, $assoc = false)
{
return cJSON::__rawjsondecode($input, $assoc);
}
/**
* public "static" method,
* converts generic variable into JSON string.
* cJSON::encode($input:Mixed):String
*
* @param mixed $input - Array, Standard Class, String, Float, integer, Boolean or null value to convert
* @param bool $slashes - add slashes for encode
* @return String - JSON string ($input param rappresentation)
*/
public static function encode($input, bool $slashes = false)
{
if ($slashes) {
return addslashes(cJSON::__rawjsonencode($input));
} else {
return cJSON::__rawjsonencode($input);
}
}
/**
* "private static" method,
* choose json core function or Pear Service to decode
*/
private static function __decode($input, &$assoc)
{
return json_decode($input, $assoc);
}
/**
* "private static" method,
* choose json core function or Pear Service to encode
*/
private static function __encode($input)
{
$result = json_encode($input);
return cJSON::__rawjsonconvert($result);
}
/**
* "private static" method,
* checks if JSON string is not an object or an array then returns correct value.
*/
private static function __rawjsondecode(&$input, &$assoc){
if(preg_match('#^(\[[^\a]+?\]|{[^\a]+?})$#', $input)){
return cJSON::__decode($input, $assoc);
}else{
$arr = cJSON::__decode('['.$input.']', $assoc);
return (!empty($arr) ? array_shift($arr) : array());
}
}
/**
* "private static" method,
* checks if input var is not an object or an array then converts correct value.
* NOTE: it's quite a non-sense but json_decode doesn't accept primitive variables then
* this method is "paranoia style"
*/
private static function __rawjsonencode(&$input){
if(is_array($input) || is_object($input))
$input = cJSON::__encode($input);
elseif(is_string($input) || is_float($input) || is_int($input) || is_bool($input) || is_null($input))
$input = substr(cJSON::__encode(array($input)), 1, -1);
else
$input = 'null';
return $input;
}
/**
* "private static" method,
* converts unconverted characters in correct range between 00-1f.
*/
private static function __rawjsonconvert(&$str){
$f = $r = array();
foreach(array_merge(range(0, 7), array(11), range(14, 31)) as $v) {
$f[] = chr($v);
$r[] = "\\u00".sprintf("%02x", $v);
}
return str_replace($f, $r, $str);
}
/**
* Function that converts generic variable into JSON string adding custom param "jsonok"
*
* @param mixed $data - Array, Standard Class, String, Float, integer, Boolean or null value to convert
* @param bool $bAddSlashes - add slashes for encode
* @return void
* @throws null
*/
public static function sendOk($data = [], bool $bAddSlashes = false): void
{
if (is_array($data)) {
$data = ['jsonok' => true] + $data;
} else {
$data = ['jsonok' => true, 'data' => $data];
}
if ($bAddSlashes) {
$output = addslashes(self::encode($data));
} else {
$output = self::encode($data);
}
if (isset($GLOBALS['coverage_tests_running'])) {
throw new Exception($output);
}
die($output);
}
/**
* Function that converts generic variable into JSON string adding custom param "error"
*
* @param mixed $data - Array, Standard Class, String, Float, integer, Boolean or null value to convert
* @param bool $bAddSlashes - add slashes for encode
* @return void
* @throws null
*/
public static function sendError($data = [], bool $bAddSlashes = false): void
{
if (is_array($data)) {
$data = ['error' => true] + $data;
} else {
$data = ['error' => true, 'data' => $data];
}
if ($bAddSlashes) {
$output = addslashes(self::encode($data));
} else {
$output = self::encode($data);
}
if (isset($GLOBALS['coverage_tests_running'])) {
throw new Exception($output);
}
die($output);
}
}