/**
* Constructor
*
* @param string $source String source to decode
* @param int $decodeType How objects should be decoded -- see
* {@link Zend_Json::TYPE_ARRAY} and {@link Zend_Json::TYPE_OBJECT} for
* valid values
* @return void
*/
function decode($source) //默认入口
{
// Set defaults
$this->_source = $source; // 保存原始字符串
$this->_sourceLength = strlen($source); //长度
$this->_token = EOF; // 当前token
$this->_offset = 0; //偏移量
// Set pointer at first token
$this->_getNextToken(); // 得到下一个token
return $this->_decodeValue(); // 调用真正的decode方法
}
/**
* Recursive driving rountine for supported toplevel tops
*
* @return mixed
*/
function _decodeValue()
{
switch ($this->_token)
{
case DATUM:
$result = $this->_tokenValue; // ?
$this->_getNextToken();
return $result;
break;
case LBRACE: // 左边大括号
return $this->_decodeObject(); // 表明是一个object,如数据库中buddylist
break;
case LBRACKET: //右边大括号
return $this->_decodeArray();
break;
default: //默认 EOF,[],;,:四类字符
return null;
break;
}
}
/**
* Decodes an object of the form:
* { "attribute: value, "attribute2" : value,...}
*
* If ZJsonEnoder or ZJAjax was used to encode the original object
* then a special attribute called __className which specifies a class
* name that should wrap the data contained within the encoded source.
*
* Decodes to either an array or StdClass object, based on the value of
* {@link $_decodeType}. If invalid $_decodeType present, returns as an
* array.
*
* @return array|StdClass
*/
function _decodeObject()
{
$result = array();
$tok = $this->_getNextToken();
while ($tok && $tok != RBRACE) // tok!=EOF || '}'
{
$key = $this->_tokenValue; // key is maybe a string for the reason of getNextToken() on coming across a "
$this->_getNextToken();
$tok = $this->_getNextToken();
$result[$key] = $this->_decodeValue(); // result is a two-demension Array
$tok = $this->_token;
if ($tok == RBRACE)
{
break;
}
$tok = $this->_getNextToken();
}
$this->_getNextToken();
return $result;
}
/**
* Decodes a JSON array format:
* [element, element2,...,elementN]
*
* @return array
*/
function _decodeArray()
{
$result = array();
$tok = $this->_getNextToken(); // Move past the '['
$index = 0;
while ($tok && $tok != RBRACKET) // tok!=EOF && tok!=']'
{
$result[$index++] = $this->_decodeValue(); //result is a 1-demensional Array
$tok = $this->_token;
if ($tok == RBRACKET || !$tok)
{
break;
}
$tok = $this->_getNextToken();
}
$this->_getNextToken();
return $result;
}
JSON PHP 完全注释(二)
最新推荐文章于 2023-03-08 13:50:36 发布