Zend Framework

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

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.8.0
  • Fix Version/s: 1.8.2
  • Component/s: Zend_Json
  • Labels:
    None

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

{"plugins":["____0_1"]}

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
	);
}
  1. Json.php
    27/May/09 8:01 AM
    14 kB
    Oscar Reales
  2. Json.php
    23/May/09 2:59 AM
    14 kB
    Markus Thielen

Activity

Hide
Markus Thielen added a comment -

patches Json.php file

Show
Markus Thielen added a comment - patches Json.php file
Hide
Matthew Weier O'Phinney added a comment -

Assigning to Oscar.

Show
Matthew Weier O'Phinney added a comment - Assigning to Oscar.
Hide
Dolf Schimmel (Freeaqingme) added a comment -

I encountered this behaviour too this week... confirmed

Show
Dolf Schimmel (Freeaqingme) added a comment - I encountered this behaviour too this week... confirmed
Hide
Oscar Reales added a comment -

working on it!

Show
Oscar Reales added a comment - working on it!
Hide
Oscar Reales added a comment -

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:

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":

//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:

"magicKey" =>  Zend_Json_Encoder::encodeUnicodeString($magicKey),

for this:

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

Show
Oscar Reales added a comment - 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:
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":
//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:
"magicKey" =>  Zend_Json_Encoder::encodeUnicodeString($magicKey),
for this:
"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.
Hide
Oscar Reales added a comment -

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

Show
Oscar Reales added a comment - 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).

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
Not Specified
Original Estimate - Not Specified
Remaining:
0m
Remaining Estimate - 0 minutes
Logged:
30m
Time Spent - 30 minutes