Skip to content

feat: add option to change default behaviour of JSONFormatter max depth #9585

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

Merged
merged 4 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions app/Config/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ class Format extends BaseConfig
'application/xml' => 0,
'text/xml' => 0,
];

/**
* --------------------------------------------------------------------------
* JSON Formatters Depth
* --------------------------------------------------------------------------
*
* Additional options to adjust default depth JSON Encode.
*/
public int $jsonDepthOptions = 512;
}
2 changes: 1 addition & 1 deletion system/Format/JSONFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function format($data)
$options |= JSON_PRETTY_PRINT;
}

$result = json_encode($data, $options, 512);
$result = json_encode($data, $options, $config->jsonDepthOptions ?? 512);

if (! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION], true)) {
throw FormatException::forInvalidJSON(json_last_error_msg());
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:** The ``Format::$jsonDepthOptions`` options to change default behavior JSON Encode.

************
Deprecations
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/outgoing/api_responses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 formatting depth by editing **app/Config/Format.php**. The ``$jsonDepthOptions`` value defines the maximum depth, with a default of ``512``.

This is the array that is used during :doc:`Content Negotiation </incoming/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.
Expand Down
Loading