mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Json pretty print (#18726)
* Added pretty print support to Json helper * Updated documentation for `yii\helpers\Json::$prettyPrint` * Fixed line ending in \yiiunit\framework\helpers\JsonTest::testPrettyPrint for windows * Added helper-json.md link in docs/guide/README.md * Capitalized JSON in descriptions Co-authored-by: Alexander Makarov <sam@rmcreative.ru> Co-authored-by: Alexander Makarov <sam@rmcreative.ru> Co-authored-by: Bizley <pawel@positive.codes>
This commit is contained in:
@ -230,6 +230,37 @@ class JsonTest extends TestCase
|
||||
$output = Json::encode($input);
|
||||
$this->assertEquals('{"date":"2014-10-12 00:00:00.000000","timezone_type":3,"timezone":"UTC"}', $output);
|
||||
}
|
||||
|
||||
public function testPrettyPrint()
|
||||
{
|
||||
$defaultValue = Json::$prettyPrint;
|
||||
$input = ['a' => 1, 'b' => 2];
|
||||
$defOutput = '{"a":1,"b":2}';
|
||||
$ppOutput = "{\n \"a\": 1,\n \"b\": 2\n}";
|
||||
|
||||
// Test unchanged options
|
||||
Json::$prettyPrint = null;
|
||||
$output = Json::encode($input, 320);
|
||||
$this->assertEquals($defOutput, $output);
|
||||
$output = Json::encode($input, 448);
|
||||
$this->assertEquals($ppOutput, $output);
|
||||
|
||||
// Test pretty print enabled
|
||||
Json::$prettyPrint = true;
|
||||
$output = Json::encode($input, 320);
|
||||
$this->assertEquals($ppOutput, $output);
|
||||
$output = Json::encode($input, 448);
|
||||
$this->assertEquals($ppOutput, $output);
|
||||
|
||||
// Test pretty print disabled
|
||||
Json::$prettyPrint = false;
|
||||
$output = Json::encode($input, 320);
|
||||
$this->assertEquals($defOutput, $output);
|
||||
$output = Json::encode($input, 448);
|
||||
$this->assertEquals($defOutput, $output);
|
||||
|
||||
Json::$prettyPrint = $defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
class JsonModel extends DynamicModel implements \JsonSerializable
|
||||
|
Reference in New Issue
Block a user