Skip to content
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

More memory-efficient json parsing #115

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"guzzlehttp/guzzle": "7.8.1",
"guzzlehttp/uri-template": "v1.0.3",
"netresearch/jsonmapper": "4.4.1",
"lstrojny/functional-php": "1.17.0"
"lstrojny/functional-php": "1.17.0",
"halaxa/json-machine": "1.1.4"
},
"require-dev": {
"brianium/paratest": "7.3.1"
Expand Down
383 changes: 218 additions & 165 deletions composer.lock

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions src/Charts/Charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use GuzzleHttp\Client;
use GuzzleHttp\UriTemplate\UriTemplate;
use GuzzleHttp\Utils;
use Psr\Http\Message\StreamInterface;
use Seatsio\GuzzleResponseDecoder;
use Seatsio\PageFetcher;
use Seatsio\SeatsioJsonMapper;
use stdClass;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function create(string $name = null, string $venueType = null, array $cat
$request->categories = $categories;
}
$res = $this->client->post('/charts', ['json' => $request]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}
Expand Down Expand Up @@ -82,9 +82,8 @@ public function removeCategory(string $key, $categoryKey): void
public function listCategories(string $chartKey): array
{
$res = $this->client->get('/charts/' . $chartKey . '/categories');
$json = Utils::jsonDecode($res->getBody());
$mapper = SeatsioJsonMapper::create();
return array_map(function($cat) {
$json = GuzzleResponseDecoder::decodeToObject($res);
return array_map(function ($cat) {
return new Category($cat->key, $cat->label, $cat->color, $cat->accessible);
}, $json->categories);
}
Expand All @@ -103,29 +102,29 @@ public function updateCategory(string $chartKey, $categoryKey, CategoryUpdatePar
public function retrieve(string $key): Chart
{
$res = $this->client->get('/charts/' . $key);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}

public function retrieveWithEvents(string $key): Chart
{
$res = $this->client->get('/charts/' . $key . '?expand=events');
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}

public function retrievePublishedVersion(string $key): object
{
$res = $this->client->get('/charts/' . $key . '/version/published');
return Utils::jsonDecode($res->getBody());
return GuzzleResponseDecoder::decodeToObject($res);
}

public function retrieveDraftVersion(string $key): object
{
$res = $this->client->get('/charts/' . $key . '/version/draft');
return Utils::jsonDecode($res->getBody());
return GuzzleResponseDecoder::decodeToObject($res);
}

public function publishDraftVersion(string $key): void
Expand All @@ -151,31 +150,31 @@ public function moveOutOfArchive(string $key): void
public function copy(string $key): Chart
{
$res = $this->client->post('/charts/' . $key . '/version/published/actions/copy');
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}

public function copyDraftVersion(string $key): Chart
{
$res = $this->client->post('/charts/' . $key . '/version/draft/actions/copy');
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}

public function copyToWorkspace(string $chartKey, string $toWorkspaceKey): Chart
{
$res = $this->client->post('/charts/' . $chartKey . '/version/published/actions/copy-to-workspace/' . $toWorkspaceKey);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}

public function copyFromWorkspaceTo(string $chartKey, string $fromWorkspaceKey, string $toWorkspaceKey): Chart
{
$res = $this->client->post('/charts/' . $chartKey . '/version/published/actions/copy/from/' . $fromWorkspaceKey . '/to/' . $toWorkspaceKey);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Chart());
}
Expand All @@ -189,13 +188,13 @@ public function retrievePublishedVersionThumbnail(string $key): StreamInterface
public function validatePublishedVersion(string $key): object
{
$res = $this->client->post('/charts/' . $key . '/version/published/actions/validate');
return Utils::jsonDecode($res->getBody());
return GuzzleResponseDecoder::decodeToObject($res);
}

public function validateDraftVersion(string $key): object
{
$res = $this->client->post('/charts/' . $key . '/version/draft/actions/validate');
return Utils::jsonDecode($res->getBody());
return GuzzleResponseDecoder::decodeToObject($res);
}

public function retrieveDraftVersionThumbnail(string $key): StreamInterface
Expand All @@ -210,7 +209,7 @@ public function retrieveDraftVersionThumbnail(string $key): StreamInterface
public function listAllTags(): array
{
$res = $this->client->get('/charts/tags');
return Utils::jsonDecode($res->getBody())->tags;
return GuzzleResponseDecoder::decodeToObject($res)->tags;
}

public function addTag(string $key, string $tag): void
Expand Down
2 changes: 0 additions & 2 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Seatsio\Events;

use Seatsio\LocalDate;

class Event
{
/**
Expand Down
16 changes: 8 additions & 8 deletions src/Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Query;
use GuzzleHttp\UriTemplate\UriTemplate;
use GuzzleHttp\Utils;
use Seatsio\GuzzleResponseDecoder;
use Seatsio\PageFetcher;
use Seatsio\Seasons\Season;
use Seatsio\SeatsioJsonMapper;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function create(string $chartKey, CreateEventParams $params = null): Even
}

$res = $this->client->post('/events', ['json' => $request]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new Event());
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public function createMultiple(string $chartKey, array $createEventParams): arra
}

$res = $this->client->post('/events/actions/create-multiple', ['json' => $request]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToObject($res);
$mapper = SeatsioJsonMapper::create();

return $mapper->mapArray($json->events, array(), 'Seatsio\Events\Event');
Expand All @@ -133,7 +133,7 @@ public function createMultiple(string $chartKey, array $createEventParams): arra
public function retrieve(string $eventKey): Event
{
$res = $this->client->get(UriTemplate::expand('/events/{key}', array("key" => $eventKey)));
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToObject($res);
$mapper = SeatsioJsonMapper::create();
if ($json->isSeason) {
return $mapper->map($json, new Season());
Expand Down Expand Up @@ -327,7 +327,7 @@ public function retrieveObjectInfos(string $eventKey, array $objectLabels): arra
{
$options = ['query' => Query::build(["label" => $objectLabels])];
$res = $this->client->get(UriTemplate::expand('/events/{key}/objects', ["key" => $eventKey]), $options);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
$result = [];
foreach ($json as $objectLabel => $objectInfo) {
Expand Down Expand Up @@ -377,7 +377,7 @@ public function changeObjectStatus($eventKeyOrKeys, $objectOrObjects, string $st
'/events/groups/actions/change-object-status',
['json' => $request, 'query' => ['expand' => 'objects']]
);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new ChangeObjectStatusResult());
}
Expand All @@ -396,7 +396,7 @@ public function changeObjectStatusInBatch(array $statusChangeRequests): array
'/events/actions/change-object-status',
['json' => $request, 'query' => ['expand' => 'objects']]
);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToObject($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->mapArray($json->results, array(), ChangeObjectStatusResult::class);
}
Expand Down Expand Up @@ -534,7 +534,7 @@ public function changeBestAvailableObjectStatus(string $eventKey, int $number, s
UriTemplate::expand('/events/{key}/actions/change-object-status', array("key" => $eventKey)),
['json' => $request]
);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new BestAvailableObjects());
}
Expand Down
35 changes: 35 additions & 0 deletions src/GuzzleResponseDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Seatsio;

use GuzzleHttp\Psr7\StreamWrapper;
use JsonMachine\Items;

class GuzzleResponseDecoder
{

public static function decodeToJson($res)
{
return Items::fromStream(StreamWrapper::getResource($res->getBody()));
}

public static function decodeToObject($res)
{
$json = GuzzleResponseDecoder::decodeToJson($res);
$object = new \stdClass();
foreach ($json as $name => $item) {
$object->$name = $item;
}
return $object;
}

public static function decodeToArray($res)
{
$json = GuzzleResponseDecoder::decodeToJson($res);
$array = [];
foreach ($json as $name => $item) {
$array[$name] = json_decode(json_encode($item), true);
}
return $array;
}
}
8 changes: 4 additions & 4 deletions src/HoldTokens/HoldTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Seatsio\HoldTokens;

use GuzzleHttp\Client;
use GuzzleHttp\Utils;
use Seatsio\GuzzleResponseDecoder;
use Seatsio\SeatsioJsonMapper;
use stdClass;

Expand All @@ -27,7 +27,7 @@ public function create(int $expiresInMinutes = null): HoldToken
$request->expiresInMinutes = $expiresInMinutes;
}
$res = $this->client->post('/hold-tokens', ['json' => $request]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new HoldToken());
}
Expand All @@ -37,15 +37,15 @@ public function expireInMinutes(string $holdToken, int $minutes): HoldToken
$request = new stdClass();
$request->expiresInMinutes = $minutes;
$res = $this->client->post('/hold-tokens/' . $holdToken, ['json' => $request]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new HoldToken());
}

public function retrieve(string $holdToken): HoldToken
{
$res = $this->client->get('/hold-tokens/' . $holdToken);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, new HoldToken());
}
Expand Down
3 changes: 1 addition & 2 deletions src/PageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Seatsio;

use GuzzleHttp\Client;
use GuzzleHttp\Utils;

class PageFetcher
{
Expand Down Expand Up @@ -46,7 +45,7 @@ public function fetch($queryParams, $pageSize)
}
$mergedQueryParams = $this->queryParams ? array_merge($queryParams, $this->queryParams) : $queryParams;
$res = $this->client->get($this->url, ['query' => $mergedQueryParams]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, $this->pageCreator->__invoke());
}
Expand Down
9 changes: 4 additions & 5 deletions src/Reports/Charts/ChartReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Seatsio\Reports\Charts;

use GuzzleHttp\Client;
use GuzzleHttp\Utils;
use Seatsio\SeatsioJsonMapper;
use GuzzleHttp\UriTemplate\UriTemplate;
use Seatsio\Charts\ChartObjectInfo;
use function Symfony\Component\String\b;
use Seatsio\GuzzleResponseDecoder;
use Seatsio\SeatsioJsonMapper;

class ChartReports
{
Expand Down Expand Up @@ -98,14 +97,14 @@ private static function summaryReportUrl($reportType, $chartKey): string
private function getChartReport(string $reportType, string $chartKey, ?string $bookWholeTables, ?string $version): array
{
$res = $this->client->get(self::reportUrl($reportType, $chartKey), ["query" => ["bookWholeTables" => $bookWholeTables, "version" => $version]]);
$json = Utils::jsonDecode($res->getBody());
$json = GuzzleResponseDecoder::decodeToJson($res);
return $this->mapMultiValuedReport($json);
}

private function getChartSummaryReport(string $reportType, string $chartKey, ?string $bookWholeTables, ?string $version): array
{
$res = $this->client->get(self::summaryReportUrl($reportType, $chartKey), ["query" => ["bookWholeTables" => $bookWholeTables, "version" => $version]]);
return Utils::jsonDecode($res->getBody(), true);
return GuzzleResponseDecoder::decodeToArray($res);
}

/**
Expand Down
Loading
Loading