diff --git a/app/Config/Format.php b/app/Config/Format.php index 0d334d72b3fb..5bf56c22fd14 100644 --- a/app/Config/Format.php +++ b/app/Config/Format.php @@ -61,4 +61,13 @@ class Format extends BaseConfig 'application/xml' => 0, 'text/xml' => 0, ]; + + /** + * -------------------------------------------------------------------------- + * Maximum depth for JSON encoding. + * -------------------------------------------------------------------------- + * + * This value determines how deep the JSON encoder will traverse nested structures. + */ + public int $jsonEncodeDepth = 512; } diff --git a/system/Format/JSONFormatter.php b/system/Format/JSONFormatter.php index a6ba87cd724a..1ca6feb6d580 100644 --- a/system/Format/JSONFormatter.php +++ b/system/Format/JSONFormatter.php @@ -41,7 +41,7 @@ public function format($data) $options |= JSON_PRETTY_PRINT; } - $result = json_encode($data, $options, 512); + $result = json_encode($data, $options, $config->jsonEncodeDepth ?? 512); if (! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION], true)) { throw FormatException::forInvalidJSON(json_last_error_msg()); diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index ac91c4851370..dd5976d07a0f 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -85,6 +85,7 @@ Changes ******* - **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. +- **Format:** Added support for configuring ``json_encode()`` maximum depth via ``Config\Format::$jsonEncodeDepth``. ************ Deprecations diff --git a/user_guide_src/source/outgoing/api_responses.rst b/user_guide_src/source/outgoing/api_responses.rst index 8b5564aa1e98..b47c516c59e5 100644 --- a/user_guide_src/source/outgoing/api_responses.rst +++ b/user_guide_src/source/outgoing/api_responses.rst @@ -49,6 +49,8 @@ format both XML and JSON responses: .. literalinclude:: api_responses/003.php +.. 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``. + This is the array that is used during :doc:`Content Negotiation ` to determine which type of response to return. If no matches are found between what the client requested and what you support, the first format in this array is what will be returned.