| omar@php.net 2006-09-01, 6:57 pm |
| ID: 38680
Updated by: omar@php.net
Reported By: RQuadling at GMail dot com
-Status: Assigned
+Status: Bogus
Bug Type: Documentation problem
Operating System: Windows XP SP2
PHP Version: Irrelevant
Assigned To: omar
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
This is a documentation issue...
true/false/null/numerics in raw form are not valid JSON structures, so
json_decode will reject them and output NULL.
Basically, if JSON_checker (http://www.json.org/JSON_checker/) says
something is invalid JSON, then json_decode will return NULL.
json_encode emits true/false/etc as any other way makes even less
sense. :)
Previous Comments:
------------------------------------------------------------------------
[2006-09-01 13:30:31] bjori@php.net
Omar: really a doc issue?
------------------------------------------------------------------------
[2006-09-01 11:42:30] RQuadling at GMail dot com
Description:
------------
Hi.
I might be widly wrong but I would have thought that json_decode would
quite happily reverse a json_encode.
This seems to work fine for arrays and null (not tested with objects),
but doesn't work at all with strings, integers or booleans.
Though I think the NULL working is just a fluke.
I can allow for the integer to not match as there is no type info as
far as I can tell.
The documentation DOES say that ...
"Returns a string containing the JSON representation of value.
Parameters
value
The value being encoded. Can be __any__ type except a resource.
Return Values
Returns a JSON encoded string on success."
Note __any__ type.
Amendment after trying to submit bug : I see that bug#38440 relates to
this. And as this has been fixed in CVS, then I suspect this is now a
documentation issue.
Reproduce code:
---------------
<?php
$a_Originals = array
(
'SimpleString' => 'This is a string',
'Array' => array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5),
'Integer' => 1234,
'Boolean_True' => True,
'Boolean_False' => False,
'NULL' => NULL,
);
foreach($a_Originals as $s_Type => $m_Data)
{
$s_JS_Encoded = json_encode($m_Data);
$m_JS_Decoded_Array = json_decode($s_JS_Encoded, True);
echo " \n======================================
==\nEncoding $s_Type :
";
var_dump($m_Data);
echo "Encoded : ";
var_dump($s_JS_Encoded);
echo "Decoded : ";
var_dump($m_JS_Decoded_Array);
echo (serialize($m_Data) === serialize($m_JS_Decoded_Array)) ?
"\nMatches\n" : "\n**** DOES NOT MATCH ****\n";
}
?>
Expected result:
----------------
========================================
Encoding SimpleString : string(16) "This is a string"
Encoded : string(18) ""This is a string""
Decoded : string(16) "This is a string"
Matches
========================================
Encoding Array : array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Encoded : string(31) "{"a":1,"b":2,"c":3,"d":4,"e":5}"
Decoded : array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Matches
========================================
Encoding Integer : int(1234)
Encoded : string(4) "1234"
Decoded : string(1234)
**** DOES NOT MATCH ****
========================================
Encoding Boolean_True : bool(true)
Encoded : string(4) "true"
Decoded : bool(true)
Matches
========================================
Encoding Boolean_False : bool(false)
Encoded : string(5) "false"
Decoded : bool(false)
Matches
========================================
Encoding NULL : NULL
Encoded : string(4) "null"
Decoded : NULL
Matches
Actual result:
--------------
========================================
Encoding SimpleString : string(16) "This is a string"
Encoded : string(18) ""This is a string""
Decoded : NULL
**** DOES NOT MATCH ****
========================================
Encoding Array : array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Encoded : string(31) "{"a":1,"b":2,"c":3,"d":4,"e":5}"
Decoded : array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Matches
========================================
Encoding Integer : int(1234)
Encoded : string(4) "1234"
Decoded : NULL
**** DOES NOT MATCH ****
========================================
Encoding Boolean_True : bool(true)
Encoded : string(4) "true"
Decoded : NULL
**** DOES NOT MATCH ****
========================================
Encoding Boolean_False : bool(false)
Encoded : string(5) "false"
Decoded : NULL
**** DOES NOT MATCH ****
========================================
Encoding NULL : NULL
Encoded : string(4) "null"
Decoded : NULL
Matches
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=38680&edit=1
|