Issues

ZF-59: Possible Problem with Zend_Json::decode

Description

To make sure that the JSON is properly decoded, I started with a PHP array:

$fruits = array ( "fruits" => array("a" => "apple", "b" => "banana", "c" => "cantelope"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", "second", "third") );

to make sure that JSON could encode the array and then use the results of encode to pass to decode to see if I get the same results (see below). When using an associative array (Test Case 1), the array is converted to an object since JSON an object is an unordered set of name/value pairs. However, when I decode the resulting JSON text, the object is converted to an array but the rest of the arrays are dropped (Test Case 1).

When I make the associative array a numerically indexed array

$fruits = array ( "fruits" => array("apple", "banana", "cantelope"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", "second", "third") );

the Zend:JSON encoder and decoder work properly (Test Case 2).

Test Case 1 Array ( [fruits] => Array ( [a] => apple [b] => banana [c] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

{"fruits" : {"a" : "apple", "b" : "banana", "c" : "cantelope"}, "numbers" : [1, 2, 3, 4, 5, 6], "holes" : ["first", "second", "third"]}

Array ( [fruits] => Array ( [a] => apple [b] => banana [c] => cantelope ) )

Test Case 2 Array ( [fruits] => Array ( [0] => apple [1] => banana [2] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

{"fruits" : ["apple", "banana", "cantelope"], "numbers" : [1, 2, 3, 4, 5, 6], "holes" : ["first", "second", "third"]}

Array ( [fruits] => Array ( [0] => apple [1] => banana [2] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

Comments

FYI: I was checking out AJASON (http://ajason.fantastic-bits.de/index.php/…) and the JSON parser that is inlcuded in the distribution correctly handles the associative array:

Test Case 1 Array ( [fruits] => Array ( [a] => apple [b] => banana [c] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

{"fruits":{"a":"apple","b":"banana","c":"cantelope"},"numbers":[1,2,3,4,5,6],"holes":["first","second","third"]}

stdClass Object ( [fruits] => stdClass Object ( [a] => apple [b] => banana [c] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

Test Case 2 Array ( [fruits] => Array ( [0] => apple [1] => banana [2] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

{"fruits":["apple","banana","cantelope"],"numbers":[1,2,3,4,5,6],"holes":["first","second","third"]}

stdClass Object ( [fruits] => Array ( [0] => apple [1] => banana [2] => cantelope ) [numbers] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) [holes] => Array ( [0] => first [1] => second [2] => third ) )

Hi everyone,

i posted an ticket concerning this issue a few weeks ago and it appears to have been fixed: 05/31/06 12:24:18: Modified by gavin

* owner changed from zend to matthew.

06/13/06 04:59:05: Modified by matthew

* status changed from new to closed.
* resolution set to fixed.

Fixed in [620]; tested in [621].

Here is the link to the OLD ticket: [http://framework.zend.com/developer/ticket/107]

I appreciate the update; however, I just downloaded and tested the ZIP and the TAR.GZ Zend Framework, Preview 0.1.3, Released on April 18, 2006 and tested it again, and it failed again.

If it was fixed, the fix may not have been included in Preview 0.1.3 (CM mistake).

The issue was corrected post 0.1.3, and the fix will be present in 0.1.4; it is already in curren SVN.