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 #1605

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pass an int $options here and build the JSON_ option mask in the render method using whichever arguments apply. I can very easily see a future option needing support, which would mean another new argument for this function.

{
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
6 changes: 3 additions & 3 deletions Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function encodesDateTime()
{
$dateTime = \DateTime::createFromFormat('U', 86400);
$instance = $this->createInstance();
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null);
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null, false);
$this->assertEquals(86400000, $test);
}

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

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

Expand Down