ZF-10977: Allow JSON action helper to accept pre-encoded JSON

Issue Type: Improvement Created: 2011-01-21T06:20:21.000+0000 Last Updated: 2012-05-30T12:32:28.000+0000 Status: Resolved Fix version(s): - 1.12.0 (27/Aug/12)

Reporter: Duccio Gasparri (dgasparri) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Controller

  • state:patch-ready-for-review
  • zf-caretaker-adamlundrigan
  • zf-crteam-padraic
  • zf-crteam-priority
  • zf-crteam-review

Related issues: Attachments: - ZF-10977_adamlundrigan.patch


A very nice method for returning json content is to use the json ActionHelper:

<pre class="highlight">
function listAction()
  return $this->_helper->json($dataArray);

But using this method with data stored in some temporary cache requires the decoding and re-encoding of the information:

<pre class="highlight"> 
function listAction()
  return $this->_helper->json(json_decode(file_get_contents($cacheFile)));


  return $this->_helper->json(unserialize(file_get_contents($cacheFile)));

It would be faster to just json encode the data in the cache and output them with a simple:

<pre class="highlight"> 
  return $this->_helper->json(file_get_contents($cacheFile),array('jsonEncode'=>false));

It is quite easy to implement, as it requires only a couple of lines in Zend_Json::encode():

<pre class="highlight"> 
        if (isset($options['jsonEncode']) && false === $options['jsonEncode']) {
            return $valueToEncode;


Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T03:33:20.000+0000

I've attached a patch which implements the functionality you are looking for. As Zend_Controller_Action_Helper_Json doesn't use a configuration array, the usage is slightly different:

<pre class="highlight">
return $this->_helper->json(file_get_contents($cacheFile), true, false, true);

Where $this->_helper->json has the following method signuature:

<pre class="highlight">
 * Strategy pattern: call helper as helper broker method
 * Allows encoding JSON. If $sendNow is true, immediately sends JSON
 * response.
 * @param  mixed   $data
 * @param  boolean $sendNow
 * @param  boolean $keepLayouts
 * @param  boolean $dataIsJson Provided data is already JSON
 * @return string|void
public function direct($data, $sendNow = true, $keepLayouts = false, $dataIsJson = false)

The usage is a little clumsy, and I think that this action helper could be restructured to also allow setting configurations in the syntax you use in your example without breaking BC.


Posted by Pádraic Brady (padraic) on 2011-08-28T13:46:05.000+0000

I've reviewed the patch and confirm it operates as intended. Please ensure a matching unit test is included before committing.

Recommend committing to trunk (need to check with Matthew on adding to release branch as it adds to the API). Please ensure a ZF2 git pull request including matching changes is issued for ZF2 before resolving.

Posted by Duccio Gasparri ( on 2011-08-30T13:28:47.000+0000

The patch works perfectly, but I do agree that the usage could be a little clumsy. If you are worried about that, a way to mantain BC could be to check if $sendNow is an array, and then parse it for the relevant parameters. Could that work?

Posted by Adam Lundrigan (adamlundrigan) on 2011-10-19T02:14:34.000+0000

Upon further review, I missed one thing: The original patch I suggested short-circuits the calling of Zend_View_Helper_Json entirely, which means that the JSON headers never get set. I've attached an updated patch with unit tests which address this issue by extending the encoding enable/disable flag into Zend_View_Helper_Json as well. There is also an updated manual page for Zend_View_Helper_Json which explains how to disable encoding. I did not update the Zend_Controller_Action_Helper_Json manual page, as it needs a rewrite (see below).

The question of providing an array shortcut via $sendNow is related to ZF-11826. Zend_Controller_Action_Helper_Json's manual page gives incorrect examples which show an array shortcut functionality that doesn't exist in that component. Whether we choose to add that feature, or simply change the manual page, it will be done under that issue #.

Posted by Adam Lundrigan (adamlundrigan) on 2012-05-30T12:32:12.000+0000

Fixed in trunk r24829. No forward-port to ZF2 as it does not have the JSON action helper anymore.

Have you found an issue?

See the Overview section for more details.


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

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