Issues

ZF-10185: Zend_Json::prettyPrint modifies string values containing JSON array/object notation

Issue Type: Bug Created: 2010-07-20T03:41:44.000+0000 Last Updated: 2011-08-28T15:56:34.000+0000 Status: Resolved Fix version(s): - 1.11.11 (29/Sep/11)

Reporter: Adam Bridger (boogey100) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Json

  • zf-crteam-padraic
  • zf-crteam-priority

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

Description

If a string passed to the Zend_Json::prettyPrint function contains any array or object notation ([{}]) the string is modified to display in the pretty print format. I don't think this function should change a value of any of the data passed to it, but just format any actual objects/arrays.

See the following php example:

<pre class="literal">
$test = array('simple'=>'simple test string','stringwithjsonchars'=>'[1,2]');
$pretty = Zend_Json::prettyPrint(Zend_Json::encode($test), array("indent"  => " "))

print_r($pretty);

/* Actual result:
{
 "simple":"simple test string",
 "stringwithjsonchars":"[
  1,
  2
 ] "
}
*/

/* Expected result:
{
 "simple":"simple test string",
 "stringwithjsonchars":"[1,2]"
}
*/

Comments

Posted by Andrej Pavlovic (andrej.pavlovic) on 2010-12-23T07:59:52.000+0000

Confirmed in 1.11. Looks like basic regex is used in prettyPrint, which unfortunately treats all [{, the same regardless of where they appear. This function should either be removed completely or rewritten to work as promised.

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T04:34:24.000+0000

I've attached a patch which (hopefully) corrects the issue. It implements basic string-literal boundary detection and so only prints/increments the indent level when the JSON token ([{) is not inside a string literal.

Could you test the patch to see if it works as expected?

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

I've reviewed the patch and confirm it operates as intended. Please ensure a ZF2 git pull request including matching changes is issued for ZF2 before resolving.

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T15:45:29.000+0000

Committed to trunk r24420 and r24422 (oops) Merged to release-1.11 in r24421 and r24423

Created pull request against zendframework/zf2 branch master https://github.com/zendframework/zf2/pull/359

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