@@ -82,6 +82,13 @@ public function initializeArguments()
82
82
'string ' ,
83
83
'A date() format for DateTime values to JSON-compatible values. NULL means JS UNIXTIME (time()*1000) '
84
84
);
85
+ $ this ->registerArgument (
86
+ 'options ' ,
87
+ 'integer ' ,
88
+ 'Additional options to pass to json_encode, like JSON_PRETTY_PRINT ' ,
89
+ false ,
90
+ 0
91
+ );
85
92
}
86
93
87
94
/**
@@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
97
104
$ preventRecursion = (boolean ) $ arguments ['preventRecursion ' ];
98
105
$ recursionMarker = $ arguments ['recursionMarker ' ];
99
106
$ dateTimeFormat = $ arguments ['dateTimeFormat ' ];
107
+ $ options = (integer ) $ arguments ['options ' ];
100
108
static ::$ encounteredClasses = [];
101
- $ json = static ::encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat );
109
+ $ json = static ::encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat, $ options );
102
110
return $ json ;
103
111
}
104
112
@@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
108
116
* @param boolean $preventRecursion
109
117
* @param string $recursionMarker
110
118
* @param string $dateTimeFormat
119
+ * @param integer $options
111
120
* @return string
112
121
* @throws Exception
113
122
*/
114
- protected static function encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat )
123
+ protected static function encodeValue ($ value , $ useTraversableKeys , $ preventRecursion , $ recursionMarker , $ dateTimeFormat, $ options )
115
124
{
116
125
if (true === $ value instanceof \Traversable) {
117
126
// 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
128
137
$ value = static ::recursiveArrayOfDomainObjectsToArray ($ value , $ preventRecursion , $ recursionMarker );
129
138
$ value = static ::recursiveDateTimeToUnixtimeMiliseconds ($ value , $ dateTimeFormat );
130
139
}
131
- $ json = json_encode ($ value , JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG );
140
+ $ options |= JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG ;
141
+ $ json = json_encode ($ value , $ options );
132
142
if (JSON_ERROR_NONE !== json_last_error ()) {
133
143
ErrorUtility::throwViewHelperException ('The provided argument cannot be converted into JSON. ' , 1358440181 );
134
144
}
0 commit comments