Skip to content

Commit

Permalink
Deprecate the JSON view helper entirely
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jan 26, 2024
1 parent a067c44 commit fe4e6ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
41 changes: 16 additions & 25 deletions docs/book/v2/helpers/json.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Json

> WARNING: **Deprecated**
>
> The JSON view helper has been deprecated and will be removed in version 3.0.
> There is no replacement;
> however, it is trivial to encode data by using PHP's built-in [`json_encode`](https://www.php.net/json_encode) function.
When creating views that return JSON, it's important to also set the appropriate
response header. The JSON view helper does exactly that. In addition, by
default, it disables layouts (if currently enabled), as layouts generally aren't
Expand All @@ -20,28 +26,13 @@ determine how to handle the content.
<?= $this->json($this->data) ?>
```

> WARNING: **Deprecated**
>
> ### Enabling encoding using Laminas\Json\Expr
>
> **This feature of the Json view helper has been deprecated in version 2.16 and will be removed in version 3.0.**
>
> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and
> used internally to encode data.
> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr`
> objects. This option is disabled by default. To enable this option, pass a boolean `true` to the
> `enableJsonExprFinder` key of the options array:
>
> ```php
> <?= $this->json($this->data, ['enableJsonExprFinder' => true]) ?>
> ``
>
> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and
> used internally to encode data.
> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr`
> objects. This option is disabled by default. To enable this option, pass a boolean `true` to the
> `enableJsonExprFinder` key of the options array:
>
> ```php
> <?= $this->json($this->data, ['enableJsonExprFinder' => true]) ?>
> ```
### Enabling encoding using Laminas\Json\Expr

The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and used internally to encode data.
`Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr` objects.
This option is disabled by default.
To enable this option, pass a boolean `true` to the `enableJsonExprFinder` key of the options array:

```php
<?= $this->json($this->data, ['enableJsonExprFinder' => true]) ?>
```
8 changes: 3 additions & 5 deletions docs/book/v2/migration/preparing-for-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ The impact of this future removal will affect templates that use a regular short

## View Helper Changes

### [Json View Helper](../helpers/json.md)
### Deprecated View Helpers Scheduled for Removal in 3.0

In version 3, the JSON view helper will no longer set response headers _(For MVC requests)_ when used.
If you are using this feature, you will need to refactor your controllers to return the correct mime-type rather than relying on the view helper to do it for you.
The following view helpers are deprecated and will be removed in version 3.0 of `laminas-view`.

Currently, the [Json View Helper](../helpers/json.md) makes use of the [laminas-json](https://docs.laminas.dev/laminas-json/) library enabling the encoding of [JSON Expressions](https://docs.laminas.dev/laminas-json/advanced/#json-expressions).
Support for JSON expressions is being removed in version 3, so you will need to ensure that you are not using this feature prior to upgrading.
- The [Json View Helper](../helpers/json.md)
2 changes: 2 additions & 0 deletions src/Helper/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/**
* Helper for simplifying JSON responses
*
* @deprecated This view helper is obsolete and will be removed in version 3.0
*
* @psalm-suppress DeprecatedProperty
* @final
*/
Expand Down
5 changes: 5 additions & 0 deletions test/Helper/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;

/**
* @deprecated To be removed with the Json View Helper in v3.0
*
* @psalm-suppress DeprecatedClass
*/
class JsonTest extends TestCase
{
private Response $response;
Expand Down

0 comments on commit fe4e6ae

Please sign in to comment.