Skip to content

Commit

Permalink
Improve JSON converter public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 7, 2024
1 parent de5fb95 commit 49c2986
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ All Notable changes to `Csv` will be documented in this file

- `Cast*` methods accept more input type to improve Denormalization usage when `Reader::addFormatter` is used or when the collection contains data other than string and `null`.
- `Stream::getSize` is added to the internal `Stream` class
- `Stream::getContents` is added to the internal `Stream` class
- `MapIterator::toIterator` is added to the internal class `MapIterator` class to convert any `iterable` into an `Iterator`.
- Casting a CSV to an `array` it now will be a collection of array instead of a simple `array`.

Expand Down
32 changes: 27 additions & 5 deletions docs/9.0/converter/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,37 @@ the converter.
<p class="message-warning">Because we are building a <code>JSON</code> structure, the <code>JsonConverter</code> object
throws generic <code>SPL Exception</code> instead of <code>League\Csv\Exception</code>.</p>

### JsonConverter::addFlags and JsonConverter::removeFlags
### JsonConverter::withFlags and JsonConverter::withoutFlags

```php
public JsonConverter::addFlags(int ...$flag): self
public JsonConverter::removeFlags(int ...$flag): self
public JsonConverter::withFlags(int ...$flag): self
public JsonConverter::withoutFlags(int ...$flag): self
```

This method sets the JSON flags to be used during conversion. The method handles all the
flags supported by PHP `json_encode` function.

If you prefer a more expressive way for setting the flags you can use `with*` and `without*` methods
whose name are derived from PHP JSON constants.

```php
$converter = JsonConverter::create()
->withFlags(JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT)
->withoutFlags(JSON_HEX_QUOT);

//is equivalent to

$converter = JsonConverter::create()
->withPrettyPrint()
->withUnescapedSlashes()
->withForceObject()
->withoutHexQuot();
```

<p class="message-notice">Because we are converting one record at a time, the class always uses <code>JSON_THROW_ON_ERROR</code>
to stop the collection conversion. As such adding or removing the flag using the methods describe here before will
have no effect on its usage, the flag is <strong>ALWAYS</strong> set.</p>

### JsonConverter::depth

```php
Expand Down Expand Up @@ -52,7 +73,7 @@ public JsonConverter::formatter(?callback $formatter): mixed

This method allow to apply a callback prior to `json_encode` your collection individual item.
Since the encoder does not rely on PHP's `JsonSerializable` interface but on PHP's `iterable`
structure. The expected conversion may differ to what you expect. This callback allows you to
structure. The resulting conversion may differ to what you expect. This callback allows you to
specify how each item will be converted. The formatter should return a type that can be handled
by PHP `json_encode` function.

Expand Down Expand Up @@ -81,7 +102,8 @@ $document->setHeaderOffset(0);

CharsetConverter::addTo($document, 'iso-8859-15', 'utf-8');
$converter = JsonConverter::create()
->addFlags(JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES)
->withPrettyPrint()
->withUnescapedSlashes()
->depth(2)
->indentSize(2)
->formatter(function (array $row) {
Expand Down
4 changes: 4 additions & 0 deletions docs/9.0/reader/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ found records are returned as a [ResultSet](/9.0/reader/resultset) object.

### Json serialization

<p class="message-info">A dedicated <code>JsonConverter</code> class is added in version <code>9.17.0</code>
to help <a href="/9.0/converter/json/">converting CSV into proper JSON document</a> without consuming
too much memory. It is the recommended way to convert to JSON.</p>

The `Reader` class implements the `JsonSerializable` interface. As such you can use the `json_encode`
function directly on the instantiated object. The interface is implemented using PHP's
`iterator_array` on the `Reader::getRecords` method. As such, the returned `JSON`
Expand Down
4 changes: 4 additions & 0 deletions docs/9.0/reader/resultset.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ found records are returned as a [ResultSet](/9.0/reader/resultset) object.

### Json serialization

<p class="message-info">A dedicated <code>JsonConverter</code> class is added in version <code>9.17.0</code>
to help <a href="/9.0/converter/json/">converting ResultSet into proper JSON document</a> without consuming
too much memory. It is the recommended way to convert to JSON.</p>

The `ResultSet` class implements the `JsonSerializable` interface. As such you can use the `json_encode`
function directly on the instantiated object. The interface is implemented using PHP's `iterator_array`
on the `ResultSet::getRecords` method. As such, the returned `JSON` string data is affected by the
Expand Down
Loading

0 comments on commit 49c2986

Please sign in to comment.