/** * 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. Try to always use it with true ! * @return mixed Object or Array or false on error * @deprecated PHP >= 5.3 supports native json_decode * @see json_decode() */ function dol_json_decode($json, $assoc = false) { dol_syslog('dol_json_decode() is deprecated. Please update your code to use native json_decode().', LOG_WARNING); $comment = false; $out = ''; $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 if ($out != '') { eval('$array = ' . $out . ';'); } else { $array = array(); } // Return an object if (!$assoc) { if (!empty($array)) { $object = false; if (count($array) > 0) { $object = (object) array(); } foreach ($array as $key => $value) { if ($key) { $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. Try to always use it with true ! * @return mixed Object or Array */ function dol_json_decode($json, $assoc = false) { $comment = false; $out = ''; $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 if ($out != '') { eval('$array = ' . $out . ';'); } else { $array = array(); } // Return an object if (!$assoc) { if (!empty($array)) { $object = false; foreach ($array as $key => $value) { if ($key) { $object->{$key} = $value; } } return $object; } return false; } return $array; }