Skip to content

Commit f16955e

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

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
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
}

0 commit comments

Comments
 (0)