import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -18,13 +18,6 @@
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Zend_Json_Exception
|
||||
*/
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* Encode PHP constructs to JSON
|
||||
*
|
||||
@ -41,10 +34,10 @@ class Zend_Json_Encoder
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_cycleCheck;
|
||||
|
||||
|
||||
/**
|
||||
* Additional options used during encoding
|
||||
*
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
@ -122,13 +115,14 @@ class Zend_Json_Encoder
|
||||
{
|
||||
if ($this->_cycleCheck) {
|
||||
if ($this->_wasVisited($value)) {
|
||||
|
||||
|
||||
if (isset($this->_options['silenceCyclicalExceptions'])
|
||||
&& $this->_options['silenceCyclicalExceptions']===true) {
|
||||
|
||||
|
||||
return '"* RECURSION (' . get_class($value) . ') *"';
|
||||
|
||||
|
||||
} else {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception(
|
||||
'Cycles not supported in JSON encoding, cycle introduced by '
|
||||
. 'class "' . get_class($value) . '"'
|
||||
@ -140,13 +134,13 @@ class Zend_Json_Encoder
|
||||
}
|
||||
|
||||
$props = '';
|
||||
|
||||
|
||||
if ($value instanceof Iterator) {
|
||||
$propCollection = $value;
|
||||
$propCollection = $value;
|
||||
} else {
|
||||
$propCollection = get_object_vars($value);
|
||||
$propCollection = get_object_vars($value);
|
||||
}
|
||||
|
||||
|
||||
foreach ($propCollection as $name => $propValue) {
|
||||
if (isset($propValue)) {
|
||||
$props .= ','
|
||||
@ -236,6 +230,7 @@ class Zend_Json_Encoder
|
||||
|
||||
if (is_int($value) || is_float($value)) {
|
||||
$result = (string) $value;
|
||||
$result = str_replace(",", ".", $result);
|
||||
} elseif (is_string($value)) {
|
||||
$result = $this->_encodeString($value);
|
||||
} elseif (is_bool($value)) {
|
||||
@ -264,6 +259,7 @@ class Zend_Json_Encoder
|
||||
// 0x08 => \b
|
||||
// 0x0c => \f
|
||||
$string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
|
||||
$string = self::encodeUnicodeString($string);
|
||||
|
||||
return '"' . $string . '"';
|
||||
}
|
||||
@ -405,6 +401,7 @@ class Zend_Json_Encoder
|
||||
{
|
||||
$cls = new ReflectionClass($className);
|
||||
if (! $cls->isInstantiable()) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception("$className must be instantiable");
|
||||
}
|
||||
|
||||
@ -434,5 +431,143 @@ class Zend_Json_Encoder
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode Unicode Characters to \u0000 ASCII syntax.
|
||||
*
|
||||
* This algorithm was originally developed for the
|
||||
* Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com/
|
||||
* @link http://svn.solarphp.com/core/trunk/Solar/Json.php
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function encodeUnicodeString($value)
|
||||
{
|
||||
$strlen_var = strlen($value);
|
||||
$ascii = "";
|
||||
|
||||
/**
|
||||
* Iterate over every character in the string,
|
||||
* escaping with a slash or encoding to UTF-8 where necessary
|
||||
*/
|
||||
for($i = 0; $i < $strlen_var; $i++) {
|
||||
$ord_var_c = ord($value[$i]);
|
||||
|
||||
switch (true) {
|
||||
case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
|
||||
// characters U-00000000 - U-0000007F (same as ASCII)
|
||||
$ascii .= $value[$i];
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xE0) == 0xC0):
|
||||
// characters U-00000080 - U-000007FF, mask 110XXXXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c, ord($value[$i + 1]));
|
||||
$i += 1;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xF0) == 0xE0):
|
||||
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]));
|
||||
$i += 2;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xF8) == 0xF0):
|
||||
// characters U-00010000 - U-001FFFFF, mask 11110XXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]));
|
||||
$i += 3;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xFC) == 0xF8):
|
||||
// characters U-00200000 - U-03FFFFFF, mask 111110XX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]),
|
||||
ord($value[$i + 4]));
|
||||
$i += 4;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xFE) == 0xFC):
|
||||
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]),
|
||||
ord($value[$i + 4]),
|
||||
ord($value[$i + 5]));
|
||||
$i += 5;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $ascii;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string from one UTF-8 char to one UTF-16 char.
|
||||
*
|
||||
* Normally should be handled by mb_convert_encoding, but
|
||||
* provides a slower PHP-only method for installations
|
||||
* that lack the multibye string extension.
|
||||
*
|
||||
* This method is from the Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com
|
||||
* @param string $utf8 UTF-8 character
|
||||
* @return string UTF-16 character
|
||||
*/
|
||||
protected static function _utf82utf16($utf8)
|
||||
{
|
||||
// Check for mb extension otherwise do by hand.
|
||||
if( function_exists('mb_convert_encoding') ) {
|
||||
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
|
||||
}
|
||||
|
||||
switch (strlen($utf8)) {
|
||||
case 1:
|
||||
// this case should never be reached, because we are in ASCII range
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return $utf8;
|
||||
|
||||
case 2:
|
||||
// return a UTF-16 character from a 2-byte UTF-8 char
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr(0x07 & (ord($utf8{0}) >> 2))
|
||||
. chr((0xC0 & (ord($utf8{0}) << 6))
|
||||
| (0x3F & ord($utf8{1})));
|
||||
|
||||
case 3:
|
||||
// return a UTF-16 character from a 3-byte UTF-8 char
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr((0xF0 & (ord($utf8{0}) << 4))
|
||||
| (0x0F & (ord($utf8{1}) >> 2)))
|
||||
. chr((0xC0 & (ord($utf8{1}) << 6))
|
||||
| (0x7F & ord($utf8{2})));
|
||||
}
|
||||
|
||||
// ignoring UTF-32 for now, sorry
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user