PHP :: Bug #38680 :: json_decode won't decode json_encode
| Bug #38680 | json_decode won't decode json_encode | ||||
|---|---|---|---|---|---|
| Submitted: | 2006-09-01 11:42 UTC | Modified: | 2008-07-22 16:56 UTC | ||
| From: | RQuadling at GMail dot com | Assigned: | iliaa (profile) | ||
| Status: | Closed | Package: | JSON related | ||
| PHP Version: | 5.2 | OS: | |||
| Private report: | No | CVE-ID: | None | ||
[2006-09-01 11:42 UTC] 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
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2006-09-01 14:25 UTC] omar@php.net
[2006-09-01 14:43 UTC] RQuadling at GMail dot com
[2006-09-04 08:55 UTC] RQuadling at GMail dot com
[2006-09-24 00:21 UTC] philip@php.net
[2006-09-24 18:41 UTC] bjori@php.net
[2006-11-03 13:16 UTC] iliaa@php.net