ZF-6786: When encoding an non-associative array with a Zend_Json_Expr the result is the magic key: ____0
Description
This code:
$values = array();
$values['plugins'] =
array(
new Zend_Json_Expr(
'new Ext.ux.PageSizePlugin()'
)
);
return Zend_Json::encode( $values,
false,
array('enableJsonExprFinder' => true));
returns
what is obviously wrong.
I created a fix in zend/Json.php for this case. line 135:
if ($key == '') { $encodedResult = str_replace( '"' . $magicKey . '"', $value, $encodedResult ); } else { $encodedResult = str_replace( '"' . $key . '":"' . $magicKey . '"', '"' . $key . '":' . $value, $encodedResult ); }```
Comments
Posted by Markus Thielen (mthielen) on 2009-05-23T02:59:18.000+0000
patches Json.php file
Posted by Matthew Weier O'Phinney (matthew) on 2009-05-25T18:34:43.000+0000
Assigning to Oscar.
Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-05-25T18:37:41.000+0000
I encountered this behaviour too this week... confirmed ;)
Posted by Oscar Reales (oreales) on 2009-05-27T03:16:26.000+0000
working on it!
Posted by Oscar Reales (oreales) on 2009-05-27T07:51:15.000+0000
Ok this is what I get:
1.- In the pre-encoding call to _recursiveJsonExprFinder function, when a non-assoc array is passed, the "key" is setted to "null". This happen because the function Zend_Json_Encoder::encodeUnicodeString returns null when an integer is passed as param.
2.- Because of that, the after encoding replacing fails.
It could be fixed with the next patch mentioned before:
but it nis not neccessary because with the actual code "key" is not neccesary anymore. It is enough replacing "magicKey" by its correspondant "value":
Anyway, to avoid unneccesary function calls when the "currentKey" is an integer (non associative array) I changed also this code:
for this:
I have re-code "Zend_Json" and added a test to JsonTest.php to testing this bug. Test is OK now.
Posted by Oscar Reales (oreales) on 2009-05-27T08:01:07.000+0000
Re-coded Zend_Json to fix issue, and avoid unnecessary calls to Zend_Json_Encoder::encodeUnicodeString(); comments on changes included (clean old code and comments in final version).