Req #18018 [Fbk]: implement toJSON() method for objects and no header

From: Date: Wed, 03 Nov 2010 00:19:56 +0000
Subject: Req #18018 [Fbk]: implement toJSON() method for objects and no header
References: 1  Groups: php.pear.bugs 
Request: Send a blank email to pear-bugs+get-5017@lists.php.net to get a copy of this message
Edit report at http://pear.php.net/bugs/bug.php?id=18018&edit=1 ID: 18018 Updated by: alan@akbkhome.com Reported By: deusmax at gmail dot com Summary: implement toJSON() method for objects and no header Status: Feedback Type: Feature/Change Request Package: Services_JSON Operating System: N/A Package Version: 1.0.2 PHP Version: 5.3.3 Roadmap Versions: New Comment: 1) I feel that encodeUnsafe() is a confusing name. Unsafe is too general of a "Danger-danger" warning word. IF you read the doc notes on the method, it explains why it may be unsafe to use it. I think it's a very good warning. (hint XSS) - and good reminder. 2) Why does toJSON() have to return an array ? What toJSON() returns should be up to the user. This behaves exactly the same way that the stringify replace method does now - encoding the return value. 5) You could improve performance/flow by a little if you make the function_exists() calls, once in the constructor. This would avoid repetitively checking the same thing. Nice - added that. Previous Comments: ------------------------------------------------------------------------ [2010-11-02 13:36:03] deusmax Hi Alan, Thanks for a really useful package. 1) I feel that encodeUnsafe() is a confusing name. Unsafe is too general of a "Danger-danger" warning word. It is an OK name for a private method that is never seen by the user. An argument option to encode() is more versatile than a constant. If you prefer to define a constant that is fine, but it will only be used in the encode() method. 2) Why does toJSON() have to return an array ? What toJSON() returns should be up to the user. Ex. for my code, I like/want to encode js Date objects into an ISO8601 string. 3) Your code mergers the object's properties with the properties returned by toJSON(). Check if that is consistent w/ JSON2 library and Native JSON implementations. It may no be. From http://www.json.org/js.html (stringify() is the equivalent to encode()) #+BEGIN_QUOTE A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. var myJSONText = JSON.stringify(myObject, replacer); If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation. #+END_QUOTE 4) Of course, much better to use method_exists(), than get_class_methods. 5) You could improve performance/flow by a little if you make the function_exists() calls, once in the constructor. This would avoid repetitively checking the same thing. ------------------------------------------------------------------------ [2010-11-02 04:10:49] alan_k -Status: Open +Status: Feedback $x = new Services_JSON(); $x->encodeUnsafe($somevalue); I've just commited a patch for the other one. which implements it slightly differently. A) toJSON should return and array B) SERVICES_JSON_USE_TO_JSON must be used in the constructor. eg. $x = new Services_JSON(SERVICES_JSON_USE_TO_JSON); Regards Alan ------------------------------------------------------------------------ [2010-11-01 12:46:40] deusmax Description: ------------ 1) It would be nice to be able to overide the default conversion for php objects, similar feature provided by JSON2 for javascript objects. The patch checks for the method of toJSON() to be asscocioted to the class. 2) Sometimes we want to encode without sending an html header. Added a parameter to method JSON::encode($phpVar, $sendHeader = true) The following patch implements both features 236c236 < function encode($var) --- > function encode($var, $sendHeader = true) 238c238,240 < header('Content-type: application/json'); --- > if ($sendHeader) { > header('Content-type: application/json'); > } 465a468,470 > if (in_array('toJSON', get_class_methods(get_class($var)))) { > return $var->toJSON(); > } ------------------------------------------------------------------------ -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=18018&edit=1

« previous php.pear.bugs (#5017) next »