From:             RQuadling at GMail dot com
Operating system: n/a
PHP version:      5.3CVS-2009-02-24 (snap)
PHP Bug Type:     Feature/Change Request
Bug description:  Option to force empty arrays to be an empty object for 
json_encode()

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 bug report at http://bugs.php.net/?id=47493&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=47493&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=47493&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=47493&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=47493&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=47493&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=47493&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=47493&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=47493&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=47493&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=47493&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=47493&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=47493&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=47493&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=47493&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=47493&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=47493&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=47493&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=47493&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=47493&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=47493&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=47493&r=mysqlcfg

Reply via email to