Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'cs/zendframework/zendframework#6972-zend-json-psr2-comp…
Browse files Browse the repository at this point in the history
…liance' into develop

Close zendframework/zendframework#6972
Forward port zendframework/zendframework#6972
  • Loading branch information
Ocramius committed Dec 8, 2014
176 parents a2803d1 + 69ae879 + c4b012e + 9852279 + 6541083 + e4898d7 + 9c57882 + 3c03bad + 87df75a + 89e1265 + 5bc0027 + 83fd9fa + 3844e27 + a2b3a39 + cbb5bf2 + 3ef3347 + bc77506 + e5e4203 + 2a10a27 + 8ba934c + 23ca50c + fd53025 + d95e884 + fb9734f + c781722 + a76fdf6 + 7f04465 + 3bc62a9 + f574eb9 + 52d314b + bada1dc + 842e034 + b44331a + 8d1b4e9 + 59d10c2 + e630f51 + 96f4abb + bfcf88b + e2e6ee0 + bd53446 + 62fffaa + 075b3a5 + dfc876b + 2056187 + ccaac9b + 5088038 + e4fa7ad + efc7cf5 + 0f33a2d + a3589c7 + 3ddf1a3 + d86b089 + 491f302 + c0efd18 + f9e572c + 3077af2 + a811b75 + 585f923 + 9b385ff + ec1f853 + b1a5d58 + a1cf97e + c1b8314 + 1482b41 + 7d80b85 + 869bbf8 + 403ce8d + 008804b + 87522da + d31ec42 + 2f2a15a + 585cc82 + a9438e2 + d59be1f + 643e2df + a49e8f2 + 08fd26d + b52d5dc + de5abc1 + 48f2d19 + 34ca3a0 + 5169094 + 25abd15 + baa09a1 + 355680a + b2b0d91 + 6986810 + 7b1513c + 6f9a6cc + 39a1c1e + 92dbdb3 + ac3a5aa + ee15c3a + 67f414a + 49138fb + a4ebc7b + 6b11637 + f128e27 + 41746af + cc083fe + 3d54828 + ccb0392 + a8ab5b0 + 1128b0c + b5ca6d2 + 5ef536e + 6878fa7 + 66a7225 + 869024d + 82f5ac4 + a831196 + dde75d7 + 9aba77c + 8474496 + 7c3bb76 + ad8e418 + a262925 + ee212be + 7f01229 + 42758bb + e91fa40 + 21394b2 + c56da7c + 9327465 + 95794f5 + 86491b5 + b24dc4f + 295467a + dc56d1e + d4c89e8 + ddd7418 + 8d06baa + ba834b9 + 292f809 + d235382 + 2b8ddfb + a90e617 + 8897e13 + 88b1391 + dc0450d + 9fa0112 + 7b9adef + 8010d61 + 8aa284d + d94ef71 + a3e5e8a + 6601fe6 + 6290724 + 5e7c801 + d74f16a + f3c55cf + f7787b1 + e1c3e3c + 88e1629 + 3314d9a + 09dfc28 + 6eac5f2 + aa1a1a6 + c602a92 + 9275250 + 11d2caa + d3c8400 + 295642a + bcf5cb7 + 1f38df6 + 5ea4665 + c66b1db + 7869e69 + 24907b1 + 04bd745 + 4dd2224 + c288736 + 2ce8414 + 4b76f70 + a372662 + 7afe09d commit 3e125d9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 58 deletions.
36 changes: 14 additions & 22 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,17 @@ protected function _decodeArray()
return $result;
}


/**
* Removes whitespace characters from the source input
*/
protected function _eatWhitespace()
{
if (preg_match(
'/([\t\b\f\n\r ])*/s',
$this->source,
$matches,
PREG_OFFSET_CAPTURE,
$this->offset)
if (preg_match('/([\t\b\f\n\r ])*/s', $this->source, $matches, PREG_OFFSET_CAPTURE, $this->offset)
&& $matches[0][1] == $this->offset) {
$this->offset += strlen($matches[0][0]);
}
}


/**
* Retrieves the next token from the source stream
*
Expand All @@ -375,8 +368,8 @@ protected function _getNextToken()

switch ($str{$i}) {
case '{':
$this->token = self::LBRACE;
break;
$this->token = self::LBRACE;
break;
case '}':
$this->token = self::RBRACE;
break;
Expand All @@ -392,7 +385,7 @@ protected function _getNextToken()
case ':':
$this->token = self::COLON;
break;
case '"':
case '"':
$result = '';
do {
$i++;
Expand All @@ -409,31 +402,31 @@ protected function _getNextToken()
}
$chr = $str{$i};
switch ($chr) {
case '"' :
case '"':
$result .= '"';
break;
case '\\':
$result .= '\\';
break;
case '/' :
case '/':
$result .= '/';
break;
case 'b' :
case 'b':
$result .= "\x08";
break;
case 'f' :
case 'f':
$result .= "\x0c";
break;
case 'n' :
case 'n':
$result .= "\x0a";
break;
case 'r' :
case 'r':
$result .= "\x0d";
break;
case 't' :
case 't':
$result .= "\x09";
break;
case '\'' :
case '\'':
$result .= '\'';
break;
default:
Expand Down Expand Up @@ -468,7 +461,7 @@ protected function _getNextToken()
if (($i+ 3) < $strLength && substr($str, $start, 4) == "null") {
$this->token = self::DATUM;
}
$this->tokenValue = NULL;
$this->tokenValue = null;
$i += 3;
break;
}
Expand All @@ -480,8 +473,7 @@ protected function _getNextToken()

$chr = $str{$i};
if ($chr == '-' || $chr == '.' || ($chr >= '0' && $chr <= '9')) {
if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s',
$str, $matches, PREG_OFFSET_CAPTURE, $start) && $matches[0][1] == $start) {
if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s', $str, $matches, PREG_OFFSET_CAPTURE, $start) && $matches[0][1] == $start) {
$datum = $matches[0][0];

if (is_numeric($datum)) {
Expand Down
68 changes: 33 additions & 35 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected function _encodeValue(&$value)
return $this->_encodeDatum($value);
}


/**
* Encode an object to JSON by encoding each of the public properties
*
Expand Down Expand Up @@ -129,7 +128,7 @@ protected function _encodeObject(&$value)
$props = '';

if (method_exists($value, 'toJson')) {
$props =',' . preg_replace("/^\{(.*)\}$/","\\1", $value->toJson());
$props = ',' . preg_replace("/^\{(.*)\}$/", "\\1", $value->toJson());
} else {
if ($value instanceof IteratorAggregate) {
$propCollection = $value->getIterator();
Expand All @@ -155,7 +154,6 @@ protected function _encodeObject(&$value)
. $props . '}';
}


/**
* Determine if an object has been serialized already
*
Expand All @@ -171,7 +169,6 @@ protected function _wasVisited(&$value)
return false;
}


/**
* JSON encode an array value
*
Expand Down Expand Up @@ -215,7 +212,6 @@ protected function _encodeArray(&$array)
return $result;
}


/**
* JSON encode a basic data type (string, number, boolean, null)
*
Expand All @@ -241,7 +237,6 @@ protected function _encodeDatum(&$value)
return $result;
}


/**
* JSON encode a string value by escaping characters as necessary
*
Expand All @@ -265,7 +260,6 @@ protected function _encodeString(&$string)
return '"' . $string . '"';
}


/**
* Encode the constants associated with the ReflectionClass
* parameter. The encoding format is based on the class2 format
Expand All @@ -290,7 +284,6 @@ private static function _encodeConstants(ReflectionClass $cls)
return $result . "}";
}


/**
* Encode the public methods of the ReflectionClass in the
* class2 format
Expand Down Expand Up @@ -353,7 +346,6 @@ private static function _encodeMethods(ReflectionClass $cls)
return $result . "}";
}


/**
* Encode the public properties of the ReflectionClass in the class2
* format.
Expand Down Expand Up @@ -410,7 +402,6 @@ public static function encodeClass($className, $package = '')
. self::_encodeVariables($cls) .'});';
}


/**
* Encode several classes at once
*
Expand Down Expand Up @@ -471,9 +462,12 @@ public static function encodeUnicodeString($value)
case (($ordVarC & 0xF0) == 0xE0):
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2]));
$char = pack(
'C*',
$ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2])
);
$i += 2;
$utf16 = self::_utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
Expand All @@ -482,10 +476,13 @@ public static function encodeUnicodeString($value)
case (($ordVarC & 0xF8) == 0xF0):
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2]),
ord($value[$i + 3]));
$char = pack(
'C*',
$ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2]),
ord($value[$i + 3])
);
$i += 3;
$utf16 = self::_utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
Expand All @@ -494,11 +491,14 @@ public static function encodeUnicodeString($value)
case (($ordVarC & 0xFC) == 0xF8):
// characters U-00200000 - U-03FFFFFF, mask 111110XX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2]),
ord($value[$i + 3]),
ord($value[$i + 4]));
$char = pack(
'C*',
$ordVarC,
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));
Expand All @@ -507,12 +507,15 @@ public static function encodeUnicodeString($value)
case (($ordVarC & 0xFE) == 0xFC):
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ordVarC,
ord($value[$i + 1]),
ord($value[$i + 2]),
ord($value[$i + 3]),
ord($value[$i + 4]),
ord($value[$i + 5]));
$char = pack(
'C*',
$ordVarC,
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));
Expand Down Expand Up @@ -552,17 +555,12 @@ protected static function _utf82utf16($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})));
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})));
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
Expand Down
4 changes: 3 additions & 1 deletion src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public static function encode($valueToEncode, $cycleCheck = false, $options = ar
* @return mixed
*/
protected static function _recursiveJsonExprFinder(
&$value, array &$javascriptExpressions, $currentKey = null
&$value,
array &$javascriptExpressions,
$currentKey = null
) {
if ($value instanceof Expr) {
// TODO: Optimize with ascii keys, if performance is bad
Expand Down

0 comments on commit 3e125d9

Please sign in to comment.