Merge branch 'develop' of ssh://git@github.com/Dolibarr/dolibarr.git into develop
This commit is contained in:
commit
e6efef9e5c
@ -23,49 +23,62 @@
|
||||
* \ingroup core
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Implement json_encode for PHP that does not support it
|
||||
*
|
||||
* @param mixed $elements PHP Object to json encode
|
||||
* @return string Json encoded string
|
||||
*/
|
||||
function json_encode($elements)
|
||||
if (! function_exists('json_encode'))
|
||||
{
|
||||
$num = count($elements);
|
||||
|
||||
// determine type
|
||||
if (is_numeric(key($elements)))
|
||||
/**
|
||||
* Implement json_encode for PHP that does not support it
|
||||
*
|
||||
* @param mixed $elements PHP Object to json encode
|
||||
* @return string Json encoded string
|
||||
*/
|
||||
function json_encode($elements)
|
||||
{
|
||||
// indexed (list)
|
||||
$output = '[';
|
||||
for ($i = 0, $last = ($num - 1); isset($elements[$i]); ++$i)
|
||||
{
|
||||
if (is_array($elements[$i])) $output.= json_encode($elements[$i]);
|
||||
else $output .= _val($elements[$i]);
|
||||
if($i !== $last) $output.= ',';
|
||||
}
|
||||
$output.= ']';
|
||||
}
|
||||
else
|
||||
{
|
||||
// associative (object)
|
||||
$output = '{';
|
||||
$last = $num - 1;
|
||||
$i = 0;
|
||||
foreach($elements as $key => $value)
|
||||
{
|
||||
$output .= '"'.$key.'":';
|
||||
if (is_array($value)) $output.= json_encode($value);
|
||||
else $output .= _val($value);
|
||||
if ($i !== $last) $output.= ',';
|
||||
++$i;
|
||||
}
|
||||
$output.= '}';
|
||||
return dol_json_encode($elements);
|
||||
}
|
||||
}
|
||||
|
||||
// return
|
||||
return $output;
|
||||
/**
|
||||
* Implement json_encode for PHP that does not support it
|
||||
*
|
||||
* @param mixed $elements PHP Object to json encode
|
||||
* @return string Json encoded string
|
||||
*/
|
||||
function dol_json_encode($elements)
|
||||
{
|
||||
$num = count($elements);
|
||||
|
||||
// determine type
|
||||
if (is_numeric(key($elements)))
|
||||
{
|
||||
// indexed (list)
|
||||
$output = '[';
|
||||
for ($i = 0, $last = ($num - 1); isset($elements[$i]); ++$i)
|
||||
{
|
||||
if (is_array($elements[$i])) $output.= json_encode($elements[$i]);
|
||||
else $output .= _val($elements[$i]);
|
||||
if($i !== $last) $output.= ',';
|
||||
}
|
||||
$output.= ']';
|
||||
}
|
||||
else
|
||||
{
|
||||
// associative (object)
|
||||
$output = '{';
|
||||
$last = $num - 1;
|
||||
$i = 0;
|
||||
foreach($elements as $key => $value)
|
||||
{
|
||||
$output .= '"'.$key.'":';
|
||||
if (is_array($value)) $output.= json_encode($value);
|
||||
else $output .= _val($value);
|
||||
if ($i !== $last) $output.= ',';
|
||||
++$i;
|
||||
}
|
||||
$output.= '}';
|
||||
}
|
||||
|
||||
// return
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -174,58 +187,71 @@ function _val($val)
|
||||
else return 'null';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implement json_decode for PHP that does not support it
|
||||
*
|
||||
* @param string $json Json encoded to PHP Object or Array
|
||||
* @param bool $assoc False return an object, true return an array
|
||||
* @return mixed Object or Array
|
||||
*/
|
||||
function json_decode($json, $assoc=false)
|
||||
if (! function_exists('json_decode'))
|
||||
{
|
||||
$comment = false;
|
||||
|
||||
$strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length
|
||||
for ($i=0; $i<$strLength; $i++)
|
||||
/**
|
||||
* Implement json_decode for PHP that does not support it
|
||||
*
|
||||
* @param string $json Json encoded to PHP Object or Array
|
||||
* @param bool $assoc False return an object, true return an array
|
||||
* @return mixed Object or Array
|
||||
*/
|
||||
function json_decode($json, $assoc=false)
|
||||
{
|
||||
if (! $comment)
|
||||
{
|
||||
if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array(';
|
||||
else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')';
|
||||
else if ($json[$i] == ':') $out.= ' => ';
|
||||
else $out.=$json[$i];
|
||||
}
|
||||
else $out.= $json[$i];
|
||||
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
|
||||
return dol_json_decode($json, $assoc);
|
||||
}
|
||||
|
||||
$out=_unval($out);
|
||||
|
||||
// Return an array
|
||||
eval('$array = '.$out.';');
|
||||
|
||||
// Return an object
|
||||
if (! $assoc)
|
||||
{
|
||||
if (! empty($array))
|
||||
{
|
||||
$object = false;
|
||||
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
$object->{$key} = $value;
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement json_decode for PHP that does not support it
|
||||
*
|
||||
* @param string $json Json encoded to PHP Object or Array
|
||||
* @param bool $assoc False return an object, true return an array
|
||||
* @return mixed Object or Array
|
||||
*/
|
||||
function dol_json_decode($json, $assoc=false)
|
||||
{
|
||||
$comment = false;
|
||||
|
||||
$strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length
|
||||
for ($i=0; $i<$strLength; $i++)
|
||||
{
|
||||
if (! $comment)
|
||||
{
|
||||
if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array(';
|
||||
else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')';
|
||||
else if ($json[$i] == ':') $out.= ' => ';
|
||||
else $out.=$json[$i];
|
||||
}
|
||||
else $out.= $json[$i];
|
||||
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
|
||||
}
|
||||
|
||||
$out=_unval($out);
|
||||
|
||||
// Return an array
|
||||
eval('$array = '.$out.';');
|
||||
|
||||
// Return an object
|
||||
if (! $assoc)
|
||||
{
|
||||
if (! empty($array))
|
||||
{
|
||||
$object = false;
|
||||
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
$object->{$key} = $value;
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return text according to type
|
||||
@ -245,7 +271,6 @@ function _unval($val)
|
||||
return $val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* convert a string from one UTF-16 char to one UTF-8 char
|
||||
*
|
||||
@ -289,7 +314,6 @@ function utf162utf8($utf16)
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* convert a string from one UTF-8 char to one UTF-16 char
|
||||
*
|
||||
|
||||
@ -28,6 +28,7 @@ global $conf,$user,$langs,$db;
|
||||
require_once 'PHPUnit/Autoload.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/core/lib/json.lib.php';
|
||||
|
||||
if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
|
||||
if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
|
||||
@ -378,12 +379,20 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
|
||||
$this->savdb=$db;
|
||||
|
||||
$arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
|
||||
|
||||
$encoded=json_encode($arraytotest);
|
||||
//var_dump($encoded);
|
||||
$this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded);
|
||||
$decoded=json_decode($encoded,true);
|
||||
//var_dump($decoded);
|
||||
$this->assertEquals($arraytotest,$decoded);
|
||||
|
||||
$encoded=dol_json_encode($arraytotest);
|
||||
//var_dump($encoded);
|
||||
$this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded);
|
||||
$decoded=dol_json_decode($encoded,true);
|
||||
//var_dump($decoded);
|
||||
$this->assertEquals($arraytotest,$decoded);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Loading…
Reference in New Issue
Block a user