Skip to content

Instantly share code, notes, and snippets.

@muhammedbasilsk
Forked from muhqu/json_encode_unicode.php
Created September 28, 2012 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammedbasilsk/3798006 to your computer and use it in GitHub Desktop.
Save muhammedbasilsk/3798006 to your computer and use it in GitHub Desktop.
<?php
$data = (object)array(
"html" => "<foo bar=\"baz\"/> &amp;",
"arabic" => "العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī",
"hebrew" => "עִבְרִית, Ivrit",
"chinese" => "汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén",
"korean" => "한국어/조선말",
"japanese" => "日本語 Nihongo",
"umlauts" => "äüöãáàß",
"escaped" => "\u65e5\u672c\u8a9e",
"emoji" => json_decode('"\u263a \ue415\ue056\ue057\ue414\ue405\ue106\ue418 \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"'),
);
header("Content-Type: text/plain; charset=UTF-8");
print title("php: print_r(data)")."\n".print_r($data,true)."\n\n";
print title("php: json_encode(data)")."\n".($json = json_encode($data))."\nstrlen: ".strlen($json)."\n\n\n";
print title("php: json_encode_unicode(data)")."\n".($json = json_encode_unicode($data))."\nstrlen: ".strlen($json)."\n\n\n";
print title("php: print_r(json_decode(json_encode_unicode(data)))")."\n".print_r(json_decode(json_encode_unicode($data)),true)."\n\n";
print title("php: data == json_decode(json_encode_unicode(data))")."\n".print_r($data == json_decode(json_encode_unicode($data)),true)."\n\n";
function json_encode_unicode($data) {
return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
function ($m) {
$d = pack("H*", $m[1]);
$r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
return $r!=="?" && $r!=="" ? $r : $m[0];
}, json_encode($data)
);
}
// some benchmarking...
print "\n\n".title("Benchmarks")."\n";
gauge("json_encode", 1000, function() use ($data) {
json_encode($data);
});
gauge("json_encode_unicode", 1000, function() use ($data) {
json_encode_unicode($data);
});
// Utils...
function gauge($label, $times, $callback) {
$tstart = microtime(true);
for ($i=0; $i<$times; $i++) {
$callback();
}
$tend = microtime(true);
$took = ($tend-$tstart);
print (ceil($took*1000)/1000)." to run $label run $times times\n";
}
function title($str) {
return $str."\n".str_repeat("-",strlen($str));
}
?>
php: print_r(data)
------------------
stdClass Object
(
[html] => <foo bar="baz"/> &amp;
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
[hebrew] => עִבְרִית, Ivrit
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
[korean] => 한국어/조선말
[japanese] => 日本語 Nihongo
[umlauts] => äüöãáàß
[escaped] => \u65e5\u672c\u8a9e
[emoji] => ☺  😄😊😃😉😍😘
)
php: json_encode(data)
----------------------
{"html":"<foo bar=\"baz\"\/> &amp;","arabic":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629 al-\u02bfarabiyyah, IPA: [\u00e6l \u0295\u0251r\u0251\u02c8bijj\u0250], or \u0639\u0631\u0628\u064a \u02bfarab\u012b","hebrew":"\u05e2\u05b4\u05d1\u05b0\u05e8\u05b4\u05d9\u05ea, Ivrit","chinese":"\u6c49\u8bed\/\u6f22\u8a9e Hanyu; \u534e\u8bed\/\u83ef\u8a9e Hu\u00e1y\u01d4; \u4e2d\u6587 Zh\u014dngw\u00e9n","korean":"\ud55c\uad6d\uc5b4\/\uc870\uc120\ub9d0","japanese":"\u65e5\u672c\u8a9e Nihongo","umlauts":"\u00e4\u00fc\u00f6\u00e3\u00e1\u00e0\u00df","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"\u263a \ue415\ue056\ue057\ue414\ue405\ue106\ue418 \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
strlen: 708
php: json_encode_unicode(data)
------------------------------
{"html":"<foo bar=\"baz\"\/> &amp;","arabic":"العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī","hebrew":"עִבְרִית, Ivrit","chinese":"汉语\/漢語 Hanyu; 华语\/華語 Huáyǔ; 中文 Zhōngwén","korean":"한국어\/조선말","japanese":"日本語 Nihongo","umlauts":"äüöãáàß","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"☺  \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
strlen: 471
php: print_r(json_decode(json_encode_unicode(data)))
----------------------------------------------------
stdClass Object
(
[html] => <foo bar="baz"/> &amp;
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
[hebrew] => עִבְרִית, Ivrit
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
[korean] => 한국어/조선말
[japanese] => 日本語 Nihongo
[umlauts] => äüöãáàß
[escaped] => \u65e5\u672c\u8a9e
[emoji] => ☺  😄😊😃😉😍😘
)
php: data == json_decode(json_encode_unicode(data))
---------------------------------------------------
1
Benchmarks
----------
0.01 to run json_encode run 1000 times
0.658 to run json_encode_unicode run 1000 times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment