svn: /phpdoc/en/trunk/reference/json/functions/ json-decode.xml
| From: | Richard Quadling | Date: | Mon, 03 Aug 2009 12:15:33 +0000 |
| Subject: | svn: /phpdoc/en/trunk/reference/json/functions/ json-decode.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-4596@lists.php.net to get a copy of this message | ||
rquadling Mon, 03 Aug 2009 12:15:33 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=286730
Log:
Return type will be null, not false, when an error occurs. Use json_last_error() to determine exact
cause. Bug#49133.
Bug: http://bugs.php.net/49133 (Open) json_decode - return
value on failure
Changed paths:
U phpdoc/en/trunk/reference/json/functions/json-decode.xml
Modified: phpdoc/en/trunk/reference/json/functions/json-decode.xml
===================================================================
--- phpdoc/en/trunk/reference/json/functions/json-decode.xml 2009-08-03 11:59:30 UTC (rev 286729)
+++ phpdoc/en/trunk/reference/json/functions/json-decode.xml 2009-08-03 12:15:33 UTC (rev 286730)
@@ -57,7 +57,9 @@
<para>
Returns an <type>object</type> or if the optional
<parameter>assoc</parameter> parameter is &true;, an associative
- <type>array</type> is instead returned.
+ <type>array</type> is instead returned. &null; is returned if the
+ <parameter>json</parameter> cannot be decoded or if the encoded
+ data is deeper than the recursion limit.
</para>
</refsect1>
@@ -138,6 +140,72 @@
]]>
</programlisting>
</example>
+ <example>
+ <title><parameter>depth</parameter> errors</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+// Encode the data.
+$json = json_encode(
+ array(
+ 1 => array(
+ 'English' => array(
+ 'One',
+ 'January'
+ ),
+ 'French' => array(
+ 'Une',
+ 'Janvier'
+ )
+ )
+ )
+);
+
+// Define the errors.
+$json_errors = array(
+ JSON_ERROR_NONE => 'No error has occurred',
+ JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
+ JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
+ JSON_ERROR_SYNTAX => 'Syntax error',
+);
+
+// Show the errors for different depths.
+foreach(range(4, 3, -1) as $depth) {
+ var_dump(json_decode($json, True, $depth));
+ echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
+ }
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+array(1) {
+ [1]=>
+ array(2) {
+ ["English"]=>
+ array(2) {
+ [0]=>
+ string(3) "One"
+ [1]=>
+ string(7) "January"
+ }
+ ["French"]=>
+ array(2) {
+ [0]=>
+ string(3) "Une"
+ [1]=>
+ string(7) "Janvier"
+ }
+ }
+}
+Last error : No error has occurred
+
+NULL
+Last error : The maximum stack depth has been exceeded
+]]>
+ </screen>
+ </example>
</para>
</refsect1>
@@ -148,12 +216,12 @@
The JSON spec is not JavaScript, but a subset of JavaScript.
</para>
</note>
- <caution>
+ <note>
<para>
- This function will return &false; if the JSON encoded data is deeper than
- the recursion depth.
+ In the event of a failure to decode, <function>json_last_error</function>
+ can be used to determine the exact nature of the error.
</para>
- </caution>
+ </note>
</refsect1>
<refsect1 role="changelog">
@@ -187,6 +255,7 @@
<para>
<simplelist>
<member><function>json_encode</function></member>
+ <member><function>json_last_error</function></member>
</simplelist>
</para>
</refsect1>