Re: JSON inclusion in core
| From: | David Zülke | Date: | Sun, 22 Jan 2006 11:28:33 +0000 |
| Subject: | Re: JSON inclusion in core | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-21634@lists.php.net to get a copy of this message | ||
That could work. Since arrays in javascript are numeric only, one could determine if a structure is an object or an array. However, there are no functions to determine if an array contains associative items, so that would have to be implemented in userland.
Being able to encode arrays to JSON objects would have a lot of benefits. For instance, one could create JS functions or entire object structures which otherwise could not be expressed through PHP objects.
- David
Am 22.01.2006 um 09:50 schrieb Bart de Boer:
Daniel Convissor wrote:Exactly what I was going to say. The one thing I'd like to see change is having JSON "objects" decoded into PHP associative arrays, not PHP objects, as is done in the current PECL implementation. Here's what happens now via PECL JSON:Maybe introduce an optional second argument "decodetype" to json_decode() where you could pass on a constant like JSON_ARRAY or JSON_OBJECT? For example: $assoc_array = json_decode($json_string, JSON_ARRAY);$object = json_decode($json_string, JSON_OBJECT);However, I don't think such a feature should hold this from going into core.========= script ========= <?php echo '<pre>'; $enum = array('foo', 'bar'); $assoc = array('f' => 'foo', 'b' => 'bar'); $obj = new c; echo "\nInitial PHP:\n"; print_r($enum); print_r($assoc); print_r($obj); $pecl_enum = json_encode($enum); $pecl_assoc = json_encode($assoc); $pecl_obj = json_encode($obj); echo "\nJSON encoded:\n"; echo $pecl_enum . "\n"; echo $pecl_assoc . "\n"; echo $pecl_obj . "\n"; echo "\nPHP decoded from JSON:\n"; print_r(json_decode($pecl_enum)); print_r(json_decode($pecl_assoc)); print_r(json_decode($pecl_obj)); ?> ========= output ========= Initial PHP: Array (--PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php[0] => foo [1] => bar) Array ([f] => foo [b] => bar) c Object ([f] => foo [b] => bar) JSON encoded: ["foo","bar"] {"f":"foo","b":"bar"} {"f":"foo","b":"bar"} PHP decoded from JSON: Array ([0] => foo [1] => bar) stdClass Object ([f] => foo [b] => bar) stdClass Object ([f] => foo [b] => bar)