Merge branch 'develop' of ssh://git@github.com/Dolibarr/dolibarr.git into develop

This commit is contained in:
Laurent Destailleur 2012-03-26 10:00:17 +02:00
commit e6efef9e5c
2 changed files with 121 additions and 88 deletions

View File

@ -23,49 +23,62 @@
* \ingroup core * \ingroup core
*/ */
if (! function_exists('json_encode'))
/**
* 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)
{ {
$num = count($elements); /**
* Implement json_encode for PHP that does not support it
// determine type *
if (is_numeric(key($elements))) * @param mixed $elements PHP Object to json encode
* @return string Json encoded string
*/
function json_encode($elements)
{ {
// indexed (list) return dol_json_encode($elements);
$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; * 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'; else return 'null';
} }
if (! function_exists('json_decode'))
/**
* 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)
{ {
$comment = false; /**
* Implement json_decode for PHP that does not support it
$strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length *
for ($i=0; $i<$strLength; $i++) * @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) return dol_json_decode($json, $assoc);
{
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;
} }
/**
* 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 * Return text according to type
@ -245,7 +271,6 @@ function _unval($val)
return $val; return $val;
} }
/** /**
* convert a string from one UTF-16 char to one UTF-8 char * convert a string from one UTF-16 char to one UTF-8 char
* *
@ -289,7 +314,6 @@ function utf162utf8($utf16)
return ''; return '';
} }
/** /**
* convert a string from one UTF-8 char to one UTF-16 char * convert a string from one UTF-8 char to one UTF-16 char
* *

View File

@ -28,6 +28,7 @@ global $conf,$user,$langs,$db;
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.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/date.lib.php';
require_once dirname(__FILE__).'/../../htdocs/core/lib/json.lib.php';
if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
@ -378,12 +379,20 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$this->savdb=$db; $this->savdb=$db;
$arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
$encoded=json_encode($arraytotest); $encoded=json_encode($arraytotest);
//var_dump($encoded); //var_dump($encoded);
$this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded); $this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded);
$decoded=json_decode($encoded,true); $decoded=json_decode($encoded,true);
//var_dump($decoded); //var_dump($decoded);
$this->assertEquals($arraytotest,$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);
} }
} }
?> ?>