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 8, 2024
1 parent de5fb95 commit eaacec7
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 82 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
36 changes: 29 additions & 7 deletions docs/9.0/converter/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,38 @@ 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
### JSON encode flags

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

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

### JsonConverter::depth
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()
->addFlags(JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT)
->removeFlags(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>

### Json encode depth

```php
public JsonConverter::depth(int $depth): self
Expand All @@ -34,7 +55,7 @@ public JsonConverter::depth(int $depth): self
This method sets the JSON depth value during conversion. The method is a proxy to using the
`json_encode` depth parameter.

### JsonConverter::indentSize
### Json encode indentation

```php
public JsonConverter::indentSize(int $indentSize): self
Expand All @@ -44,15 +65,15 @@ This method sets the JSON indentation size value if you use the `JSON_PRETTY_PRI
all other situation this value stored via this method is never used. By default, the identation
size is the same as in PHP (ie : 4 characters long).

### JsonConverter::formatter
### Json encode formatter

```php
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 eaacec7

Please sign in to comment.