Index: branches/RC/core/units/general/helpers/json_helper.php =================================================================== diff -u -N --- branches/RC/core/units/general/helpers/json_helper.php (revision 11892) +++ branches/RC/core/units/general/helpers/json_helper.php (revision 0) @@ -1,165 +0,0 @@ -encode($data).')'; - } - - /** - * Converts PHP variable to JSON string - * Can convert any PHP variable with any content to correct JSON structure - * - * @param mixed $data Data to convert - * @return string - */ - function encode($data) - { - $type = $this->getType($data); - - switch ($type) { - case 'object': - $data = '{'.$this->processData($data, $type).'}'; - break; - case 'array': - $data = '['.$this->processData($data, $type).']'; - break; - default: - if (is_int($data) || is_float($data)) { - $data = (string)$data; - } elseif (is_string($data)) { - $data = '"'.$this->escape($data).'"'; - } elseif (is_bool($data)) { - $data = $data ? 'true' : 'false'; - } else { - $data = 'null'; - } - } - return $data; - } - - /** - * Enter description here... - * - * @param unknown_type $input - * @param unknown_type $type - * @return unknown - */ - function processData($data, $type) - { - $output = Array(); - // If data is an object - it should be converted as a key => value pair - - if ($type == 'object'){ - foreach($data as $key => $value) { - $output[] = '"'.$key.'": '.$this->encode($value); - } - } else { - foreach($data as $key => $value) { - $output[] = $this->encode($value); - } - } - return implode(',', $output);; - } - - /** - * Function determines type of variable - * - * @param unknown_type $data - * @return unknown - */ - function getType(&$data) - { - if (is_object($data)) { - $type = 'object'; - } elseif (is_array($data)) { - // If array is assoc it should be processed as an object - if($this->is_assoc($data)) { - $type = 'object'; - } else { - $type = 'array'; - } - } elseif (is_numeric($data)) { - $type = 'number'; - } elseif(is_string($data)) { - $type = 'string'; - } elseif(is_bool($data)) { - $type = 'boolean'; - } elseif(is_null($data)) { - $type = 'null'; - } else { - $type = 'string'; - } - return $type; - } - - /** - * Function determines if array is associative - * - * @param array $array - * @return bool - */ - function is_assoc($array) - { -// Arrays are defined as integer-indexed arrays starting at index 0, where -// the last index is (count($array) -1); any deviation from that is -// considered an associative array, and will be encoded as such (from Zend Framework). - - return !empty($array) && (array_keys($array) !== range(0, count($array) - 1)); - } - - /** - * Escapes special characters - * Function escapes ", \, /, \n and \r symbols so that not to cause JavaScript error or - * data loss - * - * @param string $string - * @return string - */ - function escape($string) - { - $string = preg_replace($this->match, $this->replace, $string); - - return $string; - - // Escape certain ASCII characters: - // 0x08 => \b - // 0x0c => \f -// return str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string); - } - } \ No newline at end of file