Fatal error: Cannot redeclare class JSON_obj in /home/json.php on line 8
Code (PHP)
<?php
// Thanks David Majnemer
// encode/decode stuff is borrowed from the Zend Framework which is released under the New BSD License
if (function_exists('json_encode') && function_exists('json_decode'))
{
class JSON_obj
{
function encode($data)
{
return json_encode($data);
}
function decode($data)
{
return json_decode($data);
}
}
}
else
{
class JSON_obj
{
/**
* Parse tokens used to decode the JSON object. These are not
* for public consumption, they are just used internally to the
* class.
*/
function JSON_obj()
{
define('EOF', 0);
define('DATUM', 1);
define('LBRACE', 2);
define('LBRACKET', 3);
define('RBRACE', 4);
define('RBRACKET', 5);
define('COMMA', 6);
define('COLON', 7);
}
/**
* Use to maintain a "pointer" to the source being decoded
*
* @var string
*/
var $_source;
/**
* Caches the source length
*
* @var int
*/
var $_sourceLength;
/**
* The offset within the souce being decoded
*
* @var int
*
*/
var $_offset;
/**
* The current token being considered in the parser cycle
*
* @var int
*/
var $_token;
function encode($value)
{
return $this->_encodeValue($value);
}
function _encodeArray(&$array)
{
$tmpArray = array();
// Check for associative array
if (!empty($array) && (array_keys($array) !== range(0, count($array) - 1)))
{
// Associative array
$result = '{';
foreach ($array as $key => $value)
{
$key = (string) $key;
$tmpArray[] = $this->_encodeString($key) . ':' . $this->_encodeValue($value);
}
$result .= implode(',', $tmpArray);
$result .= '}';
}
else
{
// Indexed array
$result = '[';
$length = count($array);
for ($i = 0; $i < $length; ++$i)
{
$tmpArray[] = $this->_encodeValue($array[$i]);
}
$result .= implode(',', $tmpArray);
$result .= ']';
}
return $result;
}
}
?>