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