note 73290 added to function.json-encode

From: Date: Fri, 16 Feb 2007 10:43:57 +0000
Subject: note 73290 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-123479@lists.php.net to get a copy of this message
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. ---- Server IP: 195.222.164.18 Probable Submitter: 85.30.227.136 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/73290 Del: integrated -- https://master.php.net/note/delete/73290/integrated Del: useless -- https://master.php.net/note/delete/73290/useless Del: bad code -- https://master.php.net/note/delete/73290/bad+code Del: spam -- https://master.php.net/note/delete/73290/spam Del: non-english -- https://master.php.net/note/delete/73290/non-english Del: in docs -- https://master.php.net/note/delete/73290/in+docs Del: other reasons-- https://master.php.net/note/delete/73290 Reject -- https://master.php.net/note/reject/73290 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123479) next »