Req #75400 [Ana]: Already As JSON

From: Date: Tue, 24 Oct 2017 23:26:50 +0000
Subject: Req #75400 [Ana]: Already As JSON
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212304@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75400&edit=1 ID: 75400 User updated by: jwalton at aretehs dot com Reported by: jwalton at aretehs dot com Summary: Already As JSON Status: Analyzed Type: Feature/Change Request Package: JSON related Operating System: Any PHP Version: 7.0.24 Assigned To: pollita Block user comment: N Private report: N New Comment: Created a performance script. php t.php json_decenc = 53.06218791008 json_enc = 18.900541067123 json_str = 0.18521690368652 function json_decenc() { global $jsondata; $data = json_encode((object)[ "result" => json_decode($jsondata) ]); } function json_enc() { global $jd; $data = json_encode((object)[ "result" => $jd ]); } function json_str() { global $jsondata; $data = '{"result":'.$jsondata.'}'; } json document is 278,567 bytes(276k) long. json_decenc (does a decode and encode of that data into a new json document) json_enc (only does an encode of already decoded data) This shows that 64% of the time is spent decoding the data as encoding that data is relatively fast. json_str is basically string appending, this is the fastest method but doesn't maintain json integrity. As for the messages in internal, I understand where they are coming from but they are assuming that people will be building these string. And while they CAN, the main draw to this is that it could be coming from a database like PostgreSQL which already maintains the integrity of the json (JSON data type can't be inserted invalidly) Previous Comments: ------------------------------------------------------------------------ [2017-10-24 08:45:26] kelunik@php.net Assigning to you, pollita, because you plan to work on it. ------------------------------------------------------------------------ [2017-10-23 23:57:37] pollita@php.net That's not unreasonable. I'll propose it to the list and see about getting it into 7.3. ------------------------------------------------------------------------ [2017-10-17 19:55:11] jwalton at aretehs dot com Description: ------------ Ability for an object to specify that it already is encoded. Example, data returned from a database may already be in JSON format, so it seems pretty pointless to decode it, only to re-encode it in another object. It could be as simple as adding a new function maybe, like, jsonRawSerialize() or something. My issue is that sometimes the the json is over 200k and it adds quite a bit of extra CPU cycles. Test script: --------------- // Example class MyJSON { public function jsonSerialize() { return '{"myobject":true}'; } } print json_encode((object)[ "result" => new MyJSON() ]); // should print // {"result"=>{"myobject"=>true}} // //right now you have to do class MyJSON2 { public function jsonSerialize() { return json_decode('{"myobject":true}'); } } print json_encode((object)[ "result" => new MyJSON2() ]); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75400&edit=1

« previous php.bugs (#212304) next »