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

Commit

Permalink
Merge 2496826 into 8ac57d8
Browse files Browse the repository at this point in the history
  • Loading branch information
rznzippy committed Apr 23, 2015
2 parents 8ac57d8 + 2496826 commit cdf16f3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/Zend/Json/Encoder.php
Expand Up @@ -255,7 +255,9 @@ protected function _encodeString(&$string)
// 0x08 => \b
// 0x0c => \f
$string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
$string = self::encodeUnicodeString($string);
if (!isset($this->options['unescapedUnicode']) || $this->options['unescapedUnicode'] != true) {
$string = self::encodeUnicodeString($string);
}

return '"' . $string . '"';
}
Expand Down
10 changes: 10 additions & 0 deletions library/Zend/Json/Json.php
Expand Up @@ -115,6 +115,7 @@ public static function encode($valueToEncode, $cycleCheck = false, $options = ar
}

$prettyPrint = (isset($options['prettyPrint']) && ($options['prettyPrint'] == true));
$unescapedUnicode = (isset($options['unescapedUnicode']) && ($options['unescapedUnicode'] == true));

// Encoding
if (function_exists('json_encode') && static::$useBuiltinEncoderDecoder !== true) {
Expand All @@ -125,10 +126,19 @@ public static function encode($valueToEncode, $cycleCheck = false, $options = ar
$prettyPrint = false;
}

if ($unescapedUnicode && defined('JSON_UNESCAPED_UNICODE')) {
$encodeOptions |= JSON_UNESCAPED_UNICODE;
$unescapedUnicode = false;
}

$encodedResult = json_encode(
$valueToEncode,
$encodeOptions
);

if ($unescapedUnicode) {
$encodedResult = Decoder::decodeUnicodeString($encodedResult);
}
} else {
$encodedResult = Encoder::encode($valueToEncode, $cycleCheck, $options);
}
Expand Down
1 change: 1 addition & 0 deletions library/Zend/View/Model/JsonModel.php
Expand Up @@ -63,6 +63,7 @@ public function serialize()

$options = array(
'prettyPrint' => $this->getOption('prettyPrint'),
'unescapedUnicode' => $this->getOption('unescapedUnicode'),
);

if (null !== $this->jsonpCallback) {
Expand Down
21 changes: 21 additions & 0 deletions tests/ZendTest/Json/JsonTest.php
Expand Up @@ -973,6 +973,27 @@ public function testEncodeWithPrettyPrint()
$this->assertSame($expected, $pretty);
}

public function testNativeJSONEncoderWillKeepUnicodeCharactersUnescaped()
{
$value = array(
'héllö wørłd' => true
);
$expected = '{"héllö wørłd":true}';

$this->assertEquals($expected, Json\Json::encode($value, false, array('unescapedUnicode' => true)));
}

public function testBuiltinJSONEncoderWillKeepUnicodeCharactersUnescaped()
{
$value = array(
'héllö wørłd' => true
);
$expected = '{"héllö wørłd":true}';

Json\Json::$useBuiltinEncoderDecoder = true;
$this->assertEquals($expected, Json\Json::encode($value, false, array('unescapedUnicode' => true)));
}

/**
* @group ZF-11167
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/ZendTest/View/Model/JsonModelTest.php
Expand Up @@ -51,4 +51,13 @@ public function testPrettyPrint()
$model = new JsonModel($array, array('prettyPrint' => true));
$this->assertEquals(Json::encode($array, false, array('prettyPrint' => true)), $model->serialize());
}

public function testUnescapedUnicode()
{
$array = array(
'руссиш' => 'руссиш',
);
$model = new JsonModel($array, array('unescapedUnicode' => true));
$this->assertEquals(Json::encode($array, false, array('unescapedUnicode' => true)), $model->serialize());
}
}

0 comments on commit cdf16f3

Please sign in to comment.