From c3744ca6b0051c2276efc2a2fb435c1a464f5417 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 22 May 2021 22:03:35 +0200 Subject: [PATCH 1/3] Add lazy JSON parsing examples using @halaxa JsonMachine library. --- docs/pages/code/lazy-json-parsing.php | 63 +++++++++++++++++++++++++++ docs/pages/usage.rst | 6 +++ 2 files changed, 69 insertions(+) create mode 100644 docs/pages/code/lazy-json-parsing.php diff --git a/docs/pages/code/lazy-json-parsing.php b/docs/pages/code/lazy-json-parsing.php new file mode 100644 index 000000000..ff7c4a6db --- /dev/null +++ b/docs/pages/code/lazy-json-parsing.php @@ -0,0 +1,63 @@ + $value) { +} + +$remoteFile = 'https://httpbin.org/anything'; + +// Parse a remote JSON file with Guzzle +$client = new \GuzzleHttp\Client(); +$response = $client->request('GET', $remoteFile); +$phpStream = \GuzzleHttp\Psr7\StreamWrapper::getResource($response->getBody()); + +$json = Collection::fromIterable(\JsonMachine\JsonMachine::fromStream($phpStream)); + +foreach ($json as $key => $value) { +} + +// Parse a remote JSON file with Guzzle +$client = HttpClient::create(); +$response = $client->request('GET', $remoteFile); + +$json = Collection::fromIterable( + JsonMachine::fromIterable( + Collection::fromIterable($client->stream($response)) + ->map( + static fn (ChunkInterface $chunk): string => $chunk->getContent() + ) + ) +); + +foreach ($json as $key => $value) { +} diff --git a/docs/pages/usage.rst b/docs/pages/usage.rst index 93a5fdab7..e815b1c16 100644 --- a/docs/pages/usage.rst +++ b/docs/pages/usage.rst @@ -144,3 +144,9 @@ Read a file .. literalinclude:: code/read-file.php :language: php + +Lazy json parsing +~~~~~~~~~~~~~~~~~ + +.. literalinclude:: code/lazy-json-parsing.php + :language: php From 5181c4261b2fbca1662332c85ac4a80d80eaa415 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 22 May 2021 22:13:29 +0200 Subject: [PATCH 2/3] Static analysis fix - Add missing dev dependencies. --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index a011f25d6..9c45790e5 100644 --- a/composer.json +++ b/composer.json @@ -26,12 +26,15 @@ "amphp/parallel-functions": "^1", "drupol/php-conventions": "^5", "friends-of-phpspec/phpspec-code-coverage": "^6", + "guzzlehttp/guzzle": "^7.3", + "halaxa/json-machine": "^0.7.0", "infection/infection": "^0.22.0 || ^0.23.0", "infection/phpspec-adapter": "^0.1.1", "phpspec/phpspec": "^7", "phpstan/phpstan-strict-rules": "^0.12", "psr/cache": "^1.0", "symfony/cache": "^5", + "symfony/http-client": "^5.2", "symfony/polyfill-mbstring": "^1.18" }, "suggest": { From d03eb75db9507a339bf267e01346255682270484 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 23 May 2021 17:40:11 +0200 Subject: [PATCH 3/3] Fix comment. --- docs/pages/code/lazy-json-parsing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/code/lazy-json-parsing.php b/docs/pages/code/lazy-json-parsing.php index ff7c4a6db..694f4f42b 100644 --- a/docs/pages/code/lazy-json-parsing.php +++ b/docs/pages/code/lazy-json-parsing.php @@ -46,7 +46,7 @@ foreach ($json as $key => $value) { } -// Parse a remote JSON file with Guzzle +// Parse a remote JSON file with Symfony HTTP client $client = HttpClient::create(); $response = $client->request('GET', $remoteFile);