Issues

ZF-6786: When encoding an non-associative array with a Zend_Json_Expr the result is the magic key: ____0

Issue Type: Bug Created: 2009-05-23T02:57:58.000+0000 Last Updated: 2009-06-02T06:22:57.000+0000 Status: Resolved Fix version(s): - 1.8.2 (27/May/09)

Reporter: Markus Thielen (mthielen) Assignee: Oscar Reales (oreales) Tags: - Zend_Json

Related issues: Attachments: - Json.php

Description

This code:

<pre class="highlight">$values = array();
$values['plugins'] = 
  array(
    new Zend_Json_Expr(
      'new Ext.ux.PageSizePlugin()'
    )
  );

return Zend_Json::encode(   $values,
                    false,
                    array('enableJsonExprFinder' => true));

returns

<pre class="highlight">

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:

<pre class="highlight">
if ($key == '') {
    $encodedResult = str_replace(
        '"' . $magicKey . '"',
        $value,
        $encodedResult
    );
} else {
    $encodedResult = str_replace(
        '"' . $key . '":"' . $magicKey . '"',
        '"' . $key . '":' . $value,
        $encodedResult
    );
}

but it nis not neccessary because with the actual code "key" is not neccesary anymore. It is enough replacing "magicKey" by its correspondant "value":

<pre class="highlight">
        //only do post-proccessing to revert back the Zend_Json_Expr if any.
        if (count($javascriptExpressions) > 0) {
            $count = count($javascriptExpressions);
            for($i = 0; $i < $count; $i++) {
                //$key      = $javascriptExpressions[$i]['key'];//key is not neccesary
                $magicKey = $javascriptExpressions[$i]['magicKey'];
                $value    = $javascriptExpressions[$i]['value'];

                $encodedResult = str_replace(
                //instead of replacing "key:magicKey", we replace directly magicKey by value because "key" never changes.
                // '"' . $key . '":"' . $magicKey . '"',
                //'"' . $key . '":' . $value,
               
                    '"' . $magicKey . '"',
                    $value,
                    $encodedResult
                );
            }
        }

Anyway, to avoid unneccesary function calls when the "currentKey" is an integer (non associative array) I changed also this code:

<pre class="highlight">
  "magicKey" =>  Zend_Json_Encoder::encodeUnicodeString($magicKey),

for this:

<pre class="highlight">
  "magicKey" => (is_int($currentKey)) ? $magicKey : Zend_Json_Encoder::encodeUnicodeString($magicKey),

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).

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts