Skip to content

Commit

Permalink
Updated JSON Encode to reflect PHP 5.3 json_encode() flag improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
padraic committed Jul 10, 2012
1 parent a4d6ece commit bb1042a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions library/Zend/Json/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ protected function _encodeDatum(&$value)
*/
protected function _encodeString(&$string)
{
// Escape these characters with a backslash:
// Escape these characters with a backslash or unicode escape:
// " \ / \n \r \t \b \f
$search = array('\\', "\n", "\t", "\r", "\b", "\f", '"', '/');
$replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', '\\/');
$search = array('\\', "\n", "\t", "\r", "\b", "\f", '"', '\'', '&', '<', '>', '/');
$replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\\u0022', '\\u0027', '\\u0026', '\\u003C', '\\u003E', '\\/');
$string = str_replace($search, $replace, $string);

// Escape certain ASCII characters:
Expand Down
5 changes: 4 additions & 1 deletion library/Zend/Json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public static function encode($valueToEncode, $cycleCheck = false, $options = ar

// Encoding
if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) {
$encodedResult = json_encode($valueToEncode);
$encodedResult = json_encode(
$valueToEncode,
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
);
} else {
$encodedResult = Encoder::encode($valueToEncode, $cycleCheck, $options);
}
Expand Down
30 changes: 25 additions & 5 deletions tests/Zend/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public function testString4()
*/
public function testString5()
{
$expected = '"INFO: Path \"Some more\""';
$expected = '"INFO: Path \u0022Some more\u0022"';
$string = 'INFO: Path "Some more"';
$encoded = Json\Encoder::encode($string);
$this->assertEquals($expected, $encoded, 'Quote encoding incorrect: expected ' . serialize($expected) . '; received: ' . serialize($encoded) . "\n");
$this->assertEquals($string, Json\Decoder::decode($encoded));
$this->assertEquals($string, Json\Decoder::decode($encoded)); // Bug: does not accept \u0022 as token!
}

/**
Expand Down Expand Up @@ -704,7 +704,17 @@ public function testEncodeObjectImplementingIterator()
public function testNativeJSONEncoderWillProperlyEncodeSolidusInStringValues()
{
$source = "</foo><foo>bar</foo>";
$target = '"<\\/foo><foo>bar<\\/foo>"';
$target = '"\u003C\/foo\u003E\u003Cfoo\u003Ebar\u003C\/foo\u003E"';

// first test ext/json
Json\Json::$useBuiltinEncoderDecoder = false;
$this->assertEquals($target, Json\Json::encode($source));
}

public function testNativeJSONEncoderWillProperlyEncodeHtmlSpecialCharsInStringValues()
{
$source = "<>&'\"";
$target = '"\u003C\u003E\u0026\u0027\u0022"';

// first test ext/json
Json\Json::$useBuiltinEncoderDecoder = false;
Expand All @@ -717,7 +727,17 @@ public function testNativeJSONEncoderWillProperlyEncodeSolidusInStringValues()
public function testBuiltinJSONEncoderWillProperlyEncodeSolidusInStringValues()
{
$source = "</foo><foo>bar</foo>";
$target = '"<\\/foo><foo>bar<\\/foo>"';
$target = '"\u003C\/foo\u003E\u003Cfoo\u003Ebar\u003C\/foo\u003E"';

// first test ext/json
Json\Json::$useBuiltinEncoderDecoder = true;
$this->assertEquals($target, Json\Json::encode($source));
}

public function testBuiltinJSONEncoderWillProperlyEncodeHtmlSpecialCharsInStringValues()
{
$source = "<>&'\"";
$target = '"\u003C\u003E\u0026\u0027\u0022"';

// first test ext/json
Json\Json::$useBuiltinEncoderDecoder = true;
Expand Down Expand Up @@ -782,7 +802,7 @@ public function testJsonPrettyPrintWorksWithArrayNotationInStringLiteral()
$expected = <<<EOB
{
"simple":"simple test string",
"stringwithjsonchars":"\\\\\\"[1,2]",
"stringwithjsonchars":"\\\\\\u0022[1,2]",
"complex":{
"foo":"bar",
"far":"boo",
Expand Down

0 comments on commit bb1042a

Please sign in to comment.