Skip to content

Commit fa1178d

Browse files
authored
feat: add option to change default behaviour of JSONFormatter max depth (#9585)
* feat: added options for change default behaviour json depth formatter * refactor: remove variable * docs: added options jsonDepthOptions to json formatter * refactor: apply suggestion
1 parent 37445bc commit fa1178d

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

app/Config/Format.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,13 @@ class Format extends BaseConfig
6161
'application/xml' => 0,
6262
'text/xml' => 0,
6363
];
64+
65+
/**
66+
* --------------------------------------------------------------------------
67+
* Maximum depth for JSON encoding.
68+
* --------------------------------------------------------------------------
69+
*
70+
* This value determines how deep the JSON encoder will traverse nested structures.
71+
*/
72+
public int $jsonEncodeDepth = 512;
6473
}

system/Format/JSONFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function format($data)
4141
$options |= JSON_PRETTY_PRINT;
4242
}
4343

44-
$result = json_encode($data, $options, 512);
44+
$result = json_encode($data, $options, $config->jsonEncodeDepth ?? 512);
4545

4646
if (! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION], true)) {
4747
throw FormatException::forInvalidJSON(json_last_error_msg());

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Changes
8585
*******
8686

8787
- **Cookie:** The ``CookieInterface::EXPIRES_FORMAT`` has been changed to ``D, d M Y H:i:s \G\M\T`` to follow the recommended format in RFC 7231.
88+
- **Format:** Added support for configuring ``json_encode()`` maximum depth via ``Config\Format::$jsonEncodeDepth``.
8889

8990
************
9091
Deprecations

user_guide_src/source/outgoing/api_responses.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ format both XML and JSON responses:
4949

5050
.. literalinclude:: api_responses/003.php
5151

52+
.. note:: Since ``v4.7.0``, you can change the default JSON encoding depth by editing **app/Config/Format.php** file. The ``$jsonEncodeDepth`` value defines the maximum depth, with a default of ``512``.
53+
5254
This is the array that is used during :doc:`Content Negotiation </incoming/content_negotiation>` to determine which
5355
type of response to return. If no matches are found between what the client requested and what you support, the first
5456
format in this array is what will be returned.

0 commit comments

Comments
 (0)