ZF-10245: Support for ISO-8859-1 in Zend_Json::encode
Description
Please notice how easy is to introduce an option handling just for ISO-8859-1
/**
* JSON encode a string value by escaping characters as necessary
*
* @param $value string
* @return string
*/
protected function _encodeString(&$string)
{
// Escape these characters with a backslash:
// " \ / \n \r \t \b \f
$search = array('\\', "\n", "\t", "\r", "\b", "\f", '"', '/');
$replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', '\\/');
$string = str_replace($search, $replace, $string);
// Escape certain ASCII characters:
// 0x08 => \b
// 0x0c => \f
$string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
+
+ //Decode string
+ if ('ISO-8859-1' == $this->_options['encoding'])
+ $string = utf8_encode($string);
+
$string = self::encodeUnicodeString($string);
return '"' . $string . '"';
}
Comments
Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T03:50:52.000+0000
Can you provide a specific example where {{Zend_Json}} currently fails to encode an ISO-8859-1 string properly? Once I have that I can whip up a unit test to prove your fix works, and then get it committed into the project.
As you've provided code, could you also fill out a developer CLA so that we can include your code. Instructions on how to do that can be found here: http://framework.zend.com/wiki/display/…