com php-src: Allow JSON_OBJECT_AS_ARRAY option to a ctually have meaning: ext/json/json.c ext/json/test s/bug73991.phpt

[email protected] (Sara Golemon)
Newsgroups php.cvs
Message-ID <[email protected]>
Commit:    aa2282df21b32f9a2a1e86b580cb4fca5ff2c3f5
Author:    Sara Golemon <[email protected]>         Fri, 17 Mar 2017 15:36:24 -0700
Parents:   ea36cf2b89dc7c0ffedc56dfeff865ed14273063
Branches:  master

Link:       http://git.php.net/?p=php-src.git;a=commitdiff;h=aa2282df21b32f9a2a1e86b580cb4fca5ff2c3f5

Log:
Allow JSON_OBJECT_AS_ARRAY option to actually have meaning

Options can only be passed if $assoc is passed, but passing
assoc clobbers any attempt to pass JSON_OBJECT_AS_ARRAY as an
option.

Allow the option to occur in the options field by handling
"null" as default/use-options.

Changed paths:
  M  ext/json/json.c
  A  ext/json/tests/bug73991.phpt


Diff:
diff --git a/ext/json/json.c b/ext/json/json.c
index 38fc587..9803f48 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -259,13 +259,14 @@ static PHP_FUNCTION(json_decode)
 	char *str;
 	size_t str_len;
 	zend_bool assoc = 0; /* return JS objects as PHP objects by default */
+	zend_bool assoc_null = 1;
 	zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
 	zend_long options = 0;
 
 	ZEND_PARSE_PARAMETERS_START(1, 4)
 		Z_PARAM_STRING(str, str_len)
 		Z_PARAM_OPTIONAL
-		Z_PARAM_BOOL(assoc)
+		Z_PARAM_BOOL_EX(assoc, assoc_null, 1, 0)
 		Z_PARAM_LONG(depth)
 		Z_PARAM_LONG(options)
 	ZEND_PARSE_PARAMETERS_END();
@@ -288,10 +289,12 @@ static PHP_FUNCTION(json_decode)
 	}
 
 	/* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */
-	if (assoc) {
-		options |=  PHP_JSON_OBJECT_AS_ARRAY;
-	} else {
-		options &= ~PHP_JSON_OBJECT_AS_ARRAY;
+	if (!assoc_null) {
+		if (assoc) {
+			options |=  PHP_JSON_OBJECT_AS_ARRAY;
+		} else {
+			options &= ~PHP_JSON_OBJECT_AS_ARRAY;
+		}
 	}
 
 	php_json_decode_ex(return_value, str, str_len, options, depth);
diff --git a/ext/json/tests/bug73991.phpt b/ext/json/tests/bug73991.phpt
new file mode 100644
index 0000000..06aba55
--- /dev/null
+++ b/ext/json/tests/bug73991.phpt
@@ -0,0 +1,28 @@
+--TEST--
+Allow JSON_OBJECT_AS_ARRAY to have an effect
+--FILE--
+<?php
+
+$json = '{"foo":"bar"}';
+
+var_dump(json_decode($json, false));
+var_dump(json_decode($json, true));
+var_dump(json_decode($json, null, 512, 0));
+var_dump(json_decode($json, null, 512, JSON_OBJECT_AS_ARRAY));
+--EXPECTF--
+object(stdClass)#%d (1) {
+  ["foo"]=>
+  string(3) "bar"
+}
+array(1) {
+  ["foo"]=>
+  string(3) "bar"
+}
+object(stdClass)#%d (1) {
+  ["foo"]=>
+  string(3) "bar"
+}
+array(1) {
+  ["foo"]=>
+  string(3) "bar"
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.