For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > September 2006 > #38680 [NEW]: json_decode won't decode json_encode









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #38680 [NEW]: json_decode won't decode json_encode
RQuadling at GMail dot com

2006-09-01, 7:57 am

From: RQuadling at GMail dot com
Operating system: Windows XP SP2
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: json_decode won't decode json_encode

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 bug report at http://bugs.php.net/?id=38680&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38680&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38680&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38680&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=38680&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=38680&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=38680&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=38680&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=38680&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=38680&r=support
Expected behavior: http://bugs.php.net/fix.php?id=38680&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=38680&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=38680&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=38680&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38680&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=38680&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=38680&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=38680&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=38680&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=38680&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=38680&r=mysqlcfg
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com