ID: 47493
Updated by: [email protected]
Reported By: RQuadling at GMail dot com
Status: Open
Bug Type: Feature/Change Request
Operating System: n/a
PHP Version: 5.3CVS-2009-02-24 (snap)
New Comment:
In testing of forcing ALL arrays as objects and using FireBug to see
the results, a PHP array of
$array = array(
"one",
"two",
"three",
);
can be encoded as an array like ...
["one","two","three"]
or as an object like ...
{0:"one",1:"two",2,"three"}
or
{"0":"one","1":"two","2","three"}
quite happily.
Regards,
Richard Quadling.
Previous Comments:
------------------------------------------------------------------------
[2009-02-24 13:42:20] RQuadling at GMail dot com
Description:
------------
When you json_encode() an associative array you get an object hash in
Javascript.
When you json_encode() an array with numeric only keys you get an
array in Javascript.
So far so good.
But, if you json_encode() an empty array, you get an array in
Javascript, which, whilst not wrong, it is a pain if the client side
is expecting an object hash.
It would be nice to have the option to force the output to be thought
of as an object.
This would compliment the $assoc parameter of the json_decode()
function.
The flag would only be meaningful for empty arrays (I think), so the
impact SHOULD be quite low.
And as it is optional, it should not break anything backwards (but
what do I know!).
I've included below an attempt at this simply patch.
If this was added, could it be added to 5.2+.
As soon as it is added, I can update the docs for this new feature.
Index: json.c
===================================================================
RCS file: /repository/php-src/ext/json/json.c,v
retrieving revision 1.47
diff -u -r1.47 json.c
--- json.c 31 Dec 2008 11:12:32 -0000 1.47
+++ json.c 24 Feb 2009 13:36:23 -0000
@@ -41,6 +41,7 @@
#define PHP_JSON_HEX_AMP (1<<1)
#define PHP_JSON_HEX_APOS (1<<2)
#define PHP_JSON_HEX_QUOT (1<<3)
+#define PHP_JSON_FORCE_HASH (1<<4)
ZEND_DECLARE_MODULE_GLOBALS(json)
@@ -76,6 +77,8 @@
REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT,
CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("JSON_FORCE_HASH",
PHP_JSON_FORCE_HASH, CONST_CS | CONST_PERSISTENT);
+
REGISTER_LONG_CONSTANT("JSON_ERROR_NONE",
PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH",
PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_ERROR_STATE_MISMATCH",
PHP_JSON_ERROR_STATE_MISMATCH, CONST_CS | CONST_PERSISTENT);
@@ -172,7 +175,7 @@
int i, r;
HashTable *myht;
- if (Z_TYPE_PP(val) == IS_ARRAY) {
+ if (Z_TYPE_PP(val) == IS_ARRAY && !(options &
PHP_JSON_FORCE_HASH)) {
myht = HASH_OF(*val);
r = json_determine_array_type(val TSRMLS_CC);
} else {
Reproduce code:
---------------
<?php
echo json_encode(array(), PHP_JSON_FORCE_HASH);
?>
Expected result:
----------------
[]
Actual result:
--------------
{}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47493&edit=1