Issues

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

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:

$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

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.

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?

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.

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