Skip to content

Commit

Permalink
Improve JSONConverter::chunkSize implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 11, 2024
1 parent 6c773ff commit b66aef6
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class JsonConverter
/** @var Closure(string): string */
private readonly Closure $internalFormatter;
/** @var Closure(array): array */
private readonly Closure $chunkFormatter;
private readonly Closure $bufferFormatter;
/** @var int<1, max> */
public readonly int $chunkSize;

Expand All @@ -140,9 +140,9 @@ private function __construct(int $flags, int $depth, int $indentSize, ?Closure $
$this->indentSize = $indentSize;
$this->indentation = str_repeat(' ', $indentSize);
$this->formatter = $formatter ?? fn (mixed $value) => $value;
$this->isPrettyPrint = ($this->flags & JSON_PRETTY_PRINT) === JSON_PRETTY_PRINT;
$this->isForceObject = ($this->flags & JSON_FORCE_OBJECT) === JSON_FORCE_OBJECT;
$this->chunkFormatter = $this->setChunkFormatter();
$this->isPrettyPrint = $this->useFlags(JSON_PRETTY_PRINT);
$this->isForceObject = $this->useFlags(JSON_FORCE_OBJECT);
$this->bufferFormatter = $this->setChunkFormatter();
$this->internalFormatter = $this->setInternalFormatter();
$this->chunkSize = $chunkSize;
}
Expand All @@ -152,27 +152,24 @@ private function __construct(int $flags, int $depth, int $indentSize, ?Closure $
*/
private function setInternalFormatter(): Closure
{
return match ($this->isPrettyPrint) {
false => fn (string $json): string => $json,
default => $this->prettyPrint(...),
};
return $this->isPrettyPrint ? $this->prettyPrint(...) : fn (string $json): string => $json;
}

/**
* @return Closure(array): array
*/
private function setChunkFormatter(): Closure
{
return match (true) {
$this->useFlags(JSON_FORCE_OBJECT) => function (array $value): array {
return match ($this->useFlags(JSON_FORCE_OBJECT)) {
true => function (array $value): array {
$data = [];
foreach ($value as $offset => $item) {
$data[$offset] = ($this->formatter)($item, $offset);
}

return $data;
},
default => function (array $value): array {
false => function (array $value): array {
$data = [];
foreach ($value as $offset => $item) {
$data[] = ($this->formatter)($item, $offset);
Expand Down Expand Up @@ -439,7 +436,7 @@ public function convert(iterable $records): Iterator

while ($records->valid()) {
if ($incr === $this->chunkSize) {
yield $this->format($buffer).$separator;
yield $this->encodeBuffer($buffer).$separator;

$incr = 0;
$buffer = [];
Expand All @@ -453,19 +450,19 @@ public function convert(iterable $records): Iterator
}

if ([] !== $buffer) {
yield $this->format($buffer).$separator;
yield $this->encodeBuffer($buffer).$separator;
}

yield $this->format([$offset => $current]).$end;
yield $this->encodeBuffer([$offset => $current]).$end;
}

/**
* @throws JsonException
*/
private function format(array $chunk): string
private function encodeBuffer(array $buffer): string
{
$json = json_encode(
value: ($this->chunkFormatter)($chunk),
value: ($this->bufferFormatter)($buffer),
flags: ($this->flags & ~JSON_PRETTY_PRINT) | JSON_THROW_ON_ERROR,
depth: $this->depth
);
Expand Down

0 comments on commit b66aef6

Please sign in to comment.