ID: 40506 Updated by: [EMAIL PROTECTED] Reported By: php at koterov dot ru -Status: Open +Status: Bogus Bug Type: Feature/Change Request Operating System: all PHP Version: 5.2.1 New Comment:
http://www.ietf.org/rfc/rfc4627.txt?number=4627 see section 3 Previous Comments: ------------------------------------------------------------------------ [2007-02-16 10:47:31] php at koterov dot ru Description: ------------ Could you please explain why json_encode() takes care about the encoding at all? Why not to treat all the string data as a binary flow? This is very inconvenient and disallows the usage of json_encode() in non-UTF8 sites! :-( I have written a small substitution for json_encode(), but note that it of course works much more slow than json_encode() with big data arrays.. /** * Convert PHP scalar, array or hash to JS scalar/array/hash. */ function php2js($a) { if (is_null($a)) return 'null'; if ($a === false) return 'false'; if ($a === true) return 'true'; if (is_scalar($a)) { $a = addslashes($a); $a = str_replace("\n", '\n', $a); $a = str_replace("\r", '\r', $a); $a = preg_replace('{(</)(script)}i', "$1'+'$2", $a); return "'$a'"; } $isList = true; for ($i=0, reset($a); $i<count($a); $i++, next($a)) if (key($a) !== $i) { $isList = false; break; } $result = array(); if ($isList) { foreach ($a as $v) $result[] = php2js($v); return '[ ' . join(', ', $result) . ' ]'; } else { foreach ($a as $k=>$v) $result[] = php2js($k) . ': ' . php2js($v); return '{ ' . join(', ', $result) . ' }'; } } So, my suggestion is remove all string analyzation from json_encode() code. It also make this function to work faster. Reproduce code: --------------- <?php $a = array('a' => 'проверка', 'b' => array('слуха', 'глухого')); echo json_encode($a); ?> Expected result: ---------------- Correctly encoded string in the source 1-byte encoding. Actual result: -------------- Empty strings everywhere (and sometimes - notices that a string contains non-UTF8 characters). ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40506&edit=1