Skip to content

Commit

Permalink
Merge pull request #1471 from cakephp/feature/2.4-pretty-print
Browse files Browse the repository at this point in the history
pretty print json and xml responses in debug mode
  • Loading branch information
AD7six committed Aug 3, 2013
2 parents a5531f7 + 1643e10 commit f34388c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/Cake/Test/Case/View/JsonViewTest.php
Expand Up @@ -30,6 +30,11 @@
*/
class JsonViewTest extends CakeTestCase {

public function setUp() {
parent::setUp();
Configure::write('debug', 0);
}

/**
* testRenderWithoutView method
*
Expand Down
5 changes: 5 additions & 0 deletions lib/Cake/Test/Case/View/XmlViewTest.php
Expand Up @@ -30,6 +30,11 @@
*/
class XmlViewTest extends CakeTestCase {

public function setUp() {
parent::setUp();
Configure::write('debug', 0);
}

/**
* testRenderWithoutView method
*
Expand Down
5 changes: 5 additions & 0 deletions lib/Cake/View/JsonView.php
Expand Up @@ -128,6 +128,11 @@ protected function _serialize($serialize) {
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}

if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) {
return json_encode($data, JSON_PRETTY_PRINT);
}

return json_encode($data);
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Cake/View/XmlView.php
Expand Up @@ -116,7 +116,13 @@ protected function _serialize($serialize) {
$data = array($rootNode => array($serialize => $data));
}
}
return Xml::fromArray($data)->asXML();

$options = array();
if (Configure::read('debug')) {
$options['pretty'] = true;
}

return Xml::fromArray($data, $options)->asXML();
}

}

0 comments on commit f34388c

Please sign in to comment.