diff --git a/Classes/ViewHelpers/Format/Json/EncodeViewHelper.php b/Classes/ViewHelpers/Format/Json/EncodeViewHelper.php index b147b53da..87a0e3bc3 100644 --- a/Classes/ViewHelpers/Format/Json/EncodeViewHelper.php +++ b/Classes/ViewHelpers/Format/Json/EncodeViewHelper.php @@ -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( + 'options', + 'integer', + 'Additional options to pass to json_encode, like JSON_PRETTY_PRINT', + false, + 0 + ); } /** @@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl $preventRecursion = (boolean) $arguments['preventRecursion']; $recursionMarker = $arguments['recursionMarker']; $dateTimeFormat = $arguments['dateTimeFormat']; + $options = (integer) $arguments['options']; static::$encounteredClasses = []; - $json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat); + $json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $options); return $json; } @@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl * @param boolean $preventRecursion * @param string $recursionMarker * @param string $dateTimeFormat + * @param integer $options * @return string * @throws Exception */ - protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat) + protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $options) { if (true === $value instanceof \Traversable) { // Note: also converts ObjectStorage to \Vendor\Extname\Domain\Model\ObjectType[] which are each converted @@ -128,7 +137,8 @@ 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); + $options |= JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG; + $json = json_encode($value, $options); if (JSON_ERROR_NONE !== json_last_error()) { ErrorUtility::throwViewHelperException('The provided argument cannot be converted into JSON.', 1358440181); } diff --git a/Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php b/Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php index fd7cc6134..ad8538eb6 100644 --- a/Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php +++ b/Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php @@ -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, 0); $this->assertEquals(86400000, $test); } @@ -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, 0); $this->assertEquals('{"bar":"baz","children":[],"foo":null,"name":null,"pid":null,"uid":null}', $test); } @@ -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, 0); $this->assertEquals('[]', $test); }