Skip to content

Commit

Permalink
Fix: avoid phpunit warning
Browse files Browse the repository at this point in the history
  • Loading branch information
hregis committed Mar 26, 2012
1 parent 350fcf5 commit d7d5681
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 88 deletions.
200 changes: 112 additions & 88 deletions htdocs/core/lib/json.lib.php
Expand Up @@ -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)))
{
// 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
/**
* 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)
{
// 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;
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -245,7 +271,6 @@ function _unval($val)
return $val;
}


/**
* convert a string from one UTF-16 char to one UTF-8 char
*
Expand Down Expand Up @@ -289,7 +314,6 @@ function utf162utf8($utf16)
return '';
}


/**
* convert a string from one UTF-8 char to one UTF-16 char
*
Expand Down
8 changes: 8 additions & 0 deletions test/phpunit/FunctionsTest.php
Expand Up @@ -378,12 +378,20 @@ public function testJsonEncode()
$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);
}
}
?>

0 comments on commit d7d5681

Please sign in to comment.