Re: JSON inclusion in core
| From: | Bart de Boer | Date: | Sun, 22 Jan 2006 08:50:39 +0000 |
| Subject: | Re: JSON inclusion in core | ||
| References: | 1 2 3 | Groups: | php.internals php.internals |
| Request: | Send a blank email to internals+get-21633@lists.php.net to get a copy of this message | ||
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 ([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)