]> granicus.if.org Git - php/commitdiff
Use empty keys instead of _empty_ in json decoding
authorJakub Zelenka <bukka@php.net>
Sun, 29 May 2016 19:15:16 +0000 (20:15 +0100)
committerJakub Zelenka <bukka@php.net>
Mon, 20 Jun 2016 18:44:00 +0000 (19:44 +0100)
NEWS
UPGRADING
ext/json/json_encoder.c
ext/json/json_parser.tab.c
ext/json/json_parser.y
ext/json/tests/001.phpt
ext/json/tests/pass001.1_64bit.phpt
ext/json/tests/pass001.phpt

diff --git a/NEWS b/NEWS
index b2b7654d78ef1a297bc13025540eec9bc69bbc01..01a63142ebf57a0a7943083fe2d388c960a4a172 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,9 @@ PHP                                                                        NEWS
   . Fixed bug #53640 (XBM images require width to be multiple of 8). (cmb)
   . Fixed bug #64641 (imagefilledpolygon doesn't draw horizontal line). (cmb)
 
+- JSON
+  . Implemented FR #46600 ("_empty_" key in objects). (Jakub Zelenka)
+
 - Mbstring:
   . Fixed bug #72405 (mb_ereg_replace - mbc_to_code (oniguruma) -
     oob read access). (Laruence)
index 1e3532deaa705ede8e966f4b30f9d1dd0e3cb95e..69406a3c352a958996b5cf217f61aa9eee4bc584 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -46,6 +46,8 @@ PHP 7.1 UPGRADE NOTES
     the notice level only.
 
 - JSON:
+  . An empty key is decoded as an empty property name instead of using _empty_
+    property name when decoding object to stdClass.
   . When calling json_encode with JSON_UNESCAPED_UNICODE option, U+2028 and
     U+2029 are escaped.
 
index 62df1028471f804b8ba78c852172e957235ec594..f944b888c76a1f9d8ac9a2eb5bfb0dedc5c3336c 100644 (file)
@@ -169,7 +169,7 @@ static void php_json_encode_array(smart_str *buf, zval *val, int options) /* {{{
                                php_json_encode(buf, data, options);
                        } else if (r == PHP_JSON_OUTPUT_OBJECT) {
                                if (key) {
-                                       if (ZSTR_VAL(key)[0] == '\0' && Z_TYPE_P(val) == IS_OBJECT) {
+                                       if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
                                                /* Skip protected and private members. */
                                                if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
                                                        ZEND_HASH_DEC_APPLY_COUNT(tmp_ht);
index 02002d681b2b0191ba71fa9a3c82435e90793115..862912c4ec7e49965c54ab2933f46411ea4d994d 100644 (file)
@@ -1881,10 +1881,7 @@ static int php_json_parser_object_update(php_json_parser *parser, zval *object,
                zend_symtable_update(Z_ARRVAL_P(object), key, zvalue);
        } else {
                zval zkey;
-               if (ZSTR_LEN(key) == 0) {
-                       zend_string_release(key);
-                       key = zend_string_init("_empty_", sizeof("_empty_") - 1, 0);
-               } else if (ZSTR_VAL(key)[0] == '\0') {
+               if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') {
                        parser->scanner.errcode = PHP_JSON_ERROR_INVALID_PROPERTY_NAME;
                        zend_string_release(key);
                        zval_dtor(zvalue);
index fafe75cbf34497b9e1c5352ffc4667ad518b58ea..43d941b56c49f57bd9328c49dc896987ea419270 100644 (file)
@@ -273,10 +273,7 @@ static int php_json_parser_object_update(php_json_parser *parser, zval *object,
                zend_symtable_update(Z_ARRVAL_P(object), key, zvalue);
        } else {
                zval zkey;
-               if (ZSTR_LEN(key) == 0) {
-                       zend_string_release(key);
-                       key = zend_string_init("_empty_", sizeof("_empty_") - 1, 0);
-               } else if (ZSTR_VAL(key)[0] == '\0') {
+               if (ZSTR_LEN(key) > 0 && ZSTR_VAL(key)[0] == '\0') {
                        parser->scanner.errcode = PHP_JSON_ERROR_INVALID_PROPERTY_NAME;
                        zend_string_release(key);
                        zval_dtor(zvalue);
index 02d43c424390d327af3e92980f621b178fdd07f4..e908b44349f6ac79f5348a3ec8f6c44fb8ac4b74 100644 (file)
@@ -53,16 +53,16 @@ object(stdClass)#%d (1) {
   }
 }
 object(stdClass)#%d (1) {
-  ["_empty_"]=>
+  [""]=>
   object(stdClass)#%d (1) {
     ["foo"]=>
     string(0) ""
   }
 }
 object(stdClass)#%d (1) {
-  ["_empty_"]=>
+  [""]=>
   object(stdClass)#%d (1) {
-    ["_empty_"]=>
+    [""]=>
     string(0) ""
   }
 }
index e6666d15993005f3a4b9bc3b4b9be18976b6e359..3970fa434e2d94da2cc14a3316f1b70e05a5ad55 100644 (file)
@@ -204,7 +204,7 @@ array(14) {
     float(1.23456789E-13)
     ["E"]=>
     float(1.23456789E+34)
-    ["_empty_"]=>
+    [""]=>
     float(INF)
     ["E no ."]=>
     float(4000000000000)
@@ -527,7 +527,7 @@ array(14) {
   string(7) "rosebud"
 }
 ENCODE: FROM OBJECT
-["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"_empty_":0,"E no .":4000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","unicode":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","empty_string":"","true":true,"false":false,"null":null,"array":[],"object":{},"123":{"456":{"abc":{"789":"def","012":[1,2,"5",500],"ghi":[1,2,"five",50,"sixty"]}}},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
+["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"":0,"E no .":4000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","unicode":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","empty_string":"","true":true,"false":false,"null":null,"array":[],"object":{},"123":{"456":{"abc":{"789":"def","012":[1,2,"5",500],"ghi":[1,2,"five",50,"sixty"]}}},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
 ENCODE: FROM ARRAY
 ["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},[],[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"":0,"E no .":4000000000000,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","unicode":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8":"\u30d7\u30ec\u30b9\u30ad\u30c3\u30c8","empty_string":"","true":true,"false":false,"null":null,"array":[],"object":[],"123":{"456":{"abc":{"789":"def","012":[1,2,"5",500],"ghi":[1,2,"five",50,"sixty"]}}},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
 DECODE AGAIN: AS OBJECT
@@ -566,7 +566,7 @@ array(14) {
     float(1.23456789E-13)
     ["E"]=>
     float(1.23456789E+34)
-    ["_empty_"]=>
+    [""]=>
     int(0)
     ["E no ."]=>
     int(4000000000000)
index ffe7c61f01d8f24688ed61198b4bcd90e96c2bf2..948929d5a584a5b2e7a03491554edbebeb530a27 100644 (file)
@@ -188,7 +188,7 @@ array(14) {
     float(1.23456789E-13)
     ["E"]=>
     float(1.23456789E+34)
-    ["_empty_"]=>
+    [""]=>
     float(INF)
     ["zero"]=>
     int(0)
@@ -425,7 +425,7 @@ array(14) {
   string(7) "rosebud"
 }
 ENCODE: FROM OBJECT
-["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"_empty_":0,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
+["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"":0,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
 ENCODE: FROM ARRAY
 ["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},[],[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"":0,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","special":"`1~!@#$%^&*()_+-={':[,]}|;.<\/>?","hex":"\u0123\u4567\u89ab\ucdef\uabcd\uef4a","true":true,"false":false,"null":null,"array":[],"object":[],"address":"50 St. James Street","url":"http:\/\/www.JSON.org\/","comment":"\/\/ \/* <!-- --","# -- --> *\/":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\"object with 1 member\":[\"array with 1 element\"]}","quotes":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',.\/<>?":"A key can be any string"},0.5,98.6,99.44,1066,"rosebud"]
 DECODE AGAIN: AS OBJECT
@@ -464,7 +464,7 @@ array(14) {
     float(1.23456789E-13)
     ["E"]=>
     float(1.23456789E+34)
-    ["_empty_"]=>
+    [""]=>
     int(0)
     ["zero"]=>
     int(0)