Skip to content

Commit 6ec8156

Browse files
committed
Add support to pretty-print JSON-output
Optionally add JSON_PRETTY_PRINT to json_encode-options. Resolves: FluidTYPO3#1571
1 parent 4de72a2 commit 6ec8156

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

Classes/ViewHelpers/Format/Json/EncodeViewHelper.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public function initializeArguments()
8282
'string',
8383
'A date() format for DateTime values to JSON-compatible values. NULL means JS UNIXTIME (time()*1000)'
8484
);
85+
$this->registerArgument(
86+
'prettyPrint',
87+
'boolean',
88+
'If TRUE the JSON-output will be in pretty-print',
89+
false,
90+
false
91+
);
8592
}
8693

8794
/**
@@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
97104
$preventRecursion = (boolean) $arguments['preventRecursion'];
98105
$recursionMarker = $arguments['recursionMarker'];
99106
$dateTimeFormat = $arguments['dateTimeFormat'];
107+
$prettyPrint = (boolean) $arguments['prettyPrint'];
100108
static::$encounteredClasses = [];
101-
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat);
109+
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $prettyPrint);
102110
return $json;
103111
}
104112

@@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
108116
* @param boolean $preventRecursion
109117
* @param string $recursionMarker
110118
* @param string $dateTimeFormat
119+
* @param boolean $prettyPrint
111120
* @return string
112121
* @throws Exception
113122
*/
114-
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat)
123+
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $prettyPrint)
115124
{
116125
if (true === $value instanceof \Traversable) {
117126
// Note: also converts ObjectStorage to \Vendor\Extname\Domain\Model\ObjectType[] which are each converted
@@ -128,7 +137,11 @@ protected static function encodeValue($value, $useTraversableKeys, $preventRecur
128137
$value = static::recursiveArrayOfDomainObjectsToArray($value, $preventRecursion, $recursionMarker);
129138
$value = static::recursiveDateTimeToUnixtimeMiliseconds($value, $dateTimeFormat);
130139
}
131-
$json = json_encode($value, JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG);
140+
$encodeOptions = JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG;
141+
if ($prettyPrint) {
142+
$encodeOptions |= JSON_PRETTY_PRINT;
143+
}
144+
$json = json_encode($value, $encodeOptions);
132145
if (JSON_ERROR_NONE !== json_last_error()) {
133146
ErrorUtility::throwViewHelperException('The provided argument cannot be converted into JSON.', 1358440181);
134147
}

Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function encodesDateTime()
3535
{
3636
$dateTime = \DateTime::createFromFormat('U', 86400);
3737
$instance = $this->createInstance();
38-
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null);
38+
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null, false);
3939
$this->assertEquals(86400000, $test);
4040
}
4141

@@ -48,7 +48,7 @@ public function encodesRecursiveDomainObject()
4848
$object = $this->getInstanceOfFoo();
4949
$object->setFoo($object);
5050
$instance = $this->createInstance();
51-
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $object, true, true, null, null);
51+
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $object, true, true, null, null, false);
5252
$this->assertEquals('{"bar":"baz","children":[],"foo":null,"name":null,"pid":null,"uid":null}', $test);
5353
}
5454

@@ -75,7 +75,7 @@ public function encodesTraversable()
7575
{
7676
$traversable = $this->objectManager->get(ObjectStorage::class);
7777
$instance = $this->createInstance();
78-
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $traversable, false, true, null, null);
78+
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $traversable, false, true, null, null, false);
7979
$this->assertEquals('[]', $test);
8080
}
8181

0 commit comments

Comments
 (0)