reference/json/functions/json-decode.xml
ce1856e7b95807615565d2f1427582e107c450a9
...
...
@@ -11,12 +11,12 @@
11
11
<methodsynopsis>
12
12
<type>mixed</type><methodname>json_decode</methodname>
13
13
<methodparam><type>string</type><parameter>json</parameter></methodparam>
14
-
<methodparam choice="opt"><type>bool</type><parameter>assoc</parameter><initializer>&null;</initializer></methodparam>
14
+
<methodparam choice="opt"><type class="union"><type>bool</type><type>null</type></type><parameter>associative</parameter><initializer>&null;</initializer></methodparam>
15
15
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
16
-
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
16
+
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
17
17
</methodsynopsis>
18
18
<para>
19
-
Takes a JSON encoded string and converts it into a PHP variable.
19
+
Takes a JSON encoded string and converts it into a PHP value.
20
20
</para>
21
21
</refsect1>
22
22

...
...
@@ -37,14 +37,14 @@
37
37
</listitem>
38
38
</varlistentry>
39
39
<varlistentry>
40
-
<term><parameter>assoc</parameter></term>
40
+
<term><parameter>associative</parameter></term>
41
41
<listitem>
42
42
<para>
43
43
When &true;, JSON objects will be returned as
44
44
associative &array;s; when &false;, JSON objects will be returned as &object;s.
45
45
When &null;, JSON objects will be returned as associative &array;s or
46
46
&object;s depending on whether <constant>JSON_OBJECT_AS_ARRAY</constant>
47
-
is set in the <parameter>options</parameter>.
47
+
is set in the <parameter>flags</parameter>.
48
48
</para>
49
49
</listitem>
50
50
</varlistentry>
...
...
@@ -52,12 +52,14 @@
52
52
<term><parameter>depth</parameter></term>
53
53
<listitem>
54
54
<para>
55
-
User specified recursion depth.
55
+
Maximum nesting depth of the structure being decoded.
56
+
The value must be greater than <literal>0</literal>,
57
+
and less than or equal to <literal>2147483647</literal>.
56
58
</para>
57
59
</listitem>
58
60
</varlistentry>
59
61
<varlistentry>
60
-
<term><parameter>options</parameter></term>
62
+
<term><parameter>flags</parameter></term>
61
63
<listitem>
62
64
<para>
63
65
Bitmask of
...
...
@@ -78,11 +80,21 @@
78
80
<refsect1 role="returnvalues">
79
81
&reftitle.returnvalues;
80
82
<para>
81
-
Returns the value encoded in <parameter>json</parameter> in appropriate
82
-
PHP type. Values <literal>true</literal>, <literal>false</literal> and
83
-
<literal>null</literal> are returned as &true;, &false; and &null;
84
-
respectively. &null; is returned if the <parameter>json</parameter> cannot
85
-
be decoded or if the encoded data is deeper than the recursion limit.
83
+
Returns the value encoded in <parameter>json</parameter> as an appropriate
84
+
PHP type. Unquoted values <literal>true</literal>, <literal>false</literal>
85
+
and <literal>null</literal> are returned as &true;,
86
+
&false; and &null; respectively. &null; is returned if the
87
+
<parameter>json</parameter> cannot be decoded or if the encoded data is
88
+
deeper than the nesting limit.
89
+
</para>
90
+
</refsect1>
91
+

92
+
<refsect1 role="errors">
93
+
&reftitle.errors;
94
+
<para>
95
+
If <parameter>depth</parameter> is outside the allowed range,
96
+
a <classname>ValueError</classname> is thrown as of PHP 8.0.0,
97
+
while previously, an error of level <constant>E_WARNING</constant> was raised.
86
98
</para>
87
99
</refsect1>
88
100

...
...
@@ -102,13 +114,13 @@
102
114
<entry>7.3.0</entry>
103
115
<entry>
104
116
<constant>JSON_THROW_ON_ERROR</constant>
105
-
<parameter>options</parameter> was added.
117
+
<parameter>flags</parameter> was added.
106
118
</entry>
107
119
</row>
108
120
<row>
109
121
<entry>7.2.0</entry>
110
122
<entry>
111
-
<parameter>assoc</parameter> is nullable now.
123
+
<parameter>associative</parameter> is nullable now.
112
124
</entry>
113
125
</row>
114
126
<row>
...
...
@@ -116,7 +128,7 @@
116
128
<entry>
117
129
<constant>JSON_INVALID_UTF8_IGNORE</constant>, and
118
130
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>
119
-
<parameter>options</parameter> were added.
131
+
<parameter>flags</parameter> were added.
120
132
</entry>
121
133
</row>
122
134
<row>
...
...
@@ -219,7 +231,7 @@ json_decode($bad_json); // null
219
231
<programlisting role="php">
220
232
<![CDATA[
221
233
<?php
222
-
// Encode the data.
234
+
// Encode some data with a maximum depth of 4 (array -> array -> array -> string)
223
235
$json = json_encode(
224
236
array(
225
237
1 => array(
...
...
@@ -235,20 +247,12 @@ $json = json_encode(
235
247
)
236
248
);
237
249

238
-
// Define the errors.
239
-
$constants = get_defined_constants(true);
240
-
$json_errors = array();
241
-
foreach ($constants["json"] as $name => $value) {
242
-
if (!strncmp($name, "JSON_ERROR_", 11)) {
243
-
$json_errors[$value] = $name;
244
-
}
245
-
}
246
-

247
250
// Show the errors for different depths.
248
-
foreach (range(4, 3, -1) as $depth) {
249
-
var_dump(json_decode($json, true, $depth));
250
-
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
251
-
}
251
+
var_dump(json_decode($json, true, 4));
252
+
echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
253
+

254
+
var_dump(json_decode($json, true, 3));
255
+
echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
252
256
?>
253
257
]]>
254
258
</programlisting>
...
...
@@ -274,10 +278,10 @@ array(1) {
274
278
}
275
279
}
276
280
}
277
-
Last error: JSON_ERROR_NONE
281
+
Last error: No error
278
282

279
283
NULL
280
-
Last error: JSON_ERROR_DEPTH
284
+
Last error: Maximum stack depth exceeded
281
285
]]>
282
286
</screen>
283
287
</example>
...
...
@@ -336,7 +340,6 @@ object(stdClass)#1 (1) {
336
340
</para>
337
341
</refsect1>
338
342
</refentry>
339
-

340
343
<!-- Keep this comment at the end of the file
341
344
Local variables:
342
345
mode: sgml
343
346