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

Add lazy JSON parsing examples using @halaxa JsonMachine library. #93

Merged
merged 4 commits into from
May 23, 2021
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: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
63 changes: 63 additions & 0 deletions docs/pages/code/lazy-json-parsing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App;

require_once __DIR__ . '/../../../vendor/autoload.php';

use JsonMachine\JsonMachine;
use loophp\collection\Collection;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\ChunkInterface;

/**
* In order to get this working, you will need the library halaxa/json-machine.
* Use composer to install it in your project.
*
* composer require halaxa/json-machine
*
* halaxa/json-machine is a lazy JSON parser and it is fully compatible
* with loophp/collection.
*/

// Parse a local JSON file
$composerJson = __DIR__ . '/../../../composer.json';

$json = Collection::fromIterable(JsonMachine::fromFile($composerJson));

foreach ($json as $key => $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 Symfony HTTP client
$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) {
}
6 changes: 6 additions & 0 deletions docs/pages/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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