diff --git a/htdocs/core/lib/json.lib.php b/htdocs/core/lib/json.lib.php index 55b0085080fe2..0704f035848ec 100644 --- a/htdocs/core/lib/json.lib.php +++ b/htdocs/core/lib/json.lib.php @@ -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; } /** @@ -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 * diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php index 82d0010bbe884..1c6f23193d910 100755 --- a/test/phpunit/FunctionsTest.php +++ b/test/phpunit/FunctionsTest.php @@ -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); } } ?> \ No newline at end of file