Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to pretty-print JSON-output #1604

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Classes/ViewHelpers/Format/Json/EncodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public function initializeArguments()
'string',
'A date() format for DateTime values to JSON-compatible values. NULL means JS UNIXTIME (time()*1000)'
);
$this->registerArgument(
'prettyPrint',
'boolean',
'If TRUE the JSON-output will be in pretty-print',
false,
false
);
}

/**
Expand All @@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
$preventRecursion = (boolean) $arguments['preventRecursion'];
$recursionMarker = $arguments['recursionMarker'];
$dateTimeFormat = $arguments['dateTimeFormat'];
$prettyPrint = (boolean) $arguments['prettyPrint'];
static::$encounteredClasses = [];
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat);
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $prettyPrint);
return $json;
}

Expand All @@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
* @param boolean $preventRecursion
* @param string $recursionMarker
* @param string $dateTimeFormat
* @param boolean $prettyPrint
* @return string
* @throws Exception
*/
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat)
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $prettyPrint)
{
if (true === $value instanceof \Traversable) {
// Note: also converts ObjectStorage to \Vendor\Extname\Domain\Model\ObjectType[] which are each converted
Expand All @@ -128,7 +137,11 @@ protected static function encodeValue($value, $useTraversableKeys, $preventRecur
$value = static::recursiveArrayOfDomainObjectsToArray($value, $preventRecursion, $recursionMarker);
$value = static::recursiveDateTimeToUnixtimeMiliseconds($value, $dateTimeFormat);
}
$json = json_encode($value, JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG);
$encodeOptions = JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG;
if ($prettyPrint) {
$encodeOptions |= JSON_PRETTY_PRINT;
}
$json = json_encode($value, $encodeOptions);
if (JSON_ERROR_NONE !== json_last_error()) {
ErrorUtility::throwViewHelperException('The provided argument cannot be converted into JSON.', 1358440181);
}
Expand Down