1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 04:58:16 +01:00

Renamed to JsonMachine

This commit is contained in:
Filip Halaxa 2018-11-29 11:41:26 +01:00
parent 132c49bb68
commit a4143d9e61
14 changed files with 43 additions and 43 deletions

View File

@ -1,12 +1,13 @@
# JSON Iterator
# JSON Machine
Json Iterator is a simple **JSON stream parser for PHP** based on coroutines,
developed for extremely large JSON datasets. Main features are:
- Speed. Performace critical code contains no unnecessary function calls, no regular expressions
and uses native `json_decode` to decode document chunks.
- Ease of use. Just iterate it with `foreach`. No events and callbacks.
- Supports iteration on any subtree of the document, specified by [Json Pointer](https://tools.ietf.org/html/rfc6901)
Json Machine is a Fast, efficient and easy-to-use JSON stream parser based on coroutines
developed for unpredictably long JSON streams or documents. Main features are:
- Ease of use. Just iterate JSON of any size with `foreach`. No events and callbacks.
- Constant memory footprint for unpredictably large JSON documents.
- Speed. Performace critical code contains no unnecessary function calls, no regular expressions
and uses native `json_decode` to decode JSON document chunks.
- Supports efficient iteration on any subtree of the document, specified by [Json Pointer](https://tools.ietf.org/html/rfc6901)
## Examples
### Parsing simple JSON document
@ -26,7 +27,7 @@ It can be parsed this way:
```php
<?php
$jsonStream = \JsonIterator\JsonIterator::fromFile('big.json');
$jsonStream = \JsonMachine\JsonMachine::fromFile('big.json');
foreach ($jsonStream as $name => $data) {
// 1st iteration: $name === "apple" and $data === ["color" => "red"]
@ -56,7 +57,7 @@ do it like this:
```php
<?php
$jsonStream = \JsonIterator\JsonIterator::fromFile("fruits.json", "/fruits-key" /* <- Json Pointer */);
$jsonStream = \JsonMachine\JsonMachine::fromFile("fruits.json", "/fruits-key" /* <- Json Pointer */);
foreach ($jsonStream as $name => $data) {
// The same as above, which means:
// 1st iteration: $name === "apple" and $data === ["color" => "red"]
@ -64,14 +65,14 @@ foreach ($jsonStream as $name => $data) {
}
```
> Implementation detail:
> Note:
>
> Value of `fruits-key` is not loaded into memory at once, but only one item in
> `fruits-key` at a time. It is always one item in memory at a time at the level/subtree
> you are currently iterating. Thus the memory consumption is constant.
## Parsing API responses
If you use this library to parse large API responses, all you need to do is passing the stream resource
of your api response to `JsonIterator::fromStream($streamResource)`.
of your api response to `JsonMachine::fromStream($streamResource)`.
### GuzzleHttp example
Guzzle uses its own streams, but they can be converted back to PHP streams by calling
`\GuzzleHttp\Psr7\StreamWrapper::getResource()`. See [GuzzleHttp example](src/examples/guzzleHttp.php)

View File

@ -1,6 +1,6 @@
{
"name": "jsonator/jsonator",
"description": "Json parser consuming a stream of json data with easy, iterable api.",
"name": "halaxa/json-machine",
"description": "Fast, efficient and easy-to-use JSON stream parser developed for unpredictably long JSON streams or documents.",
"require": {
"php": ">=5.6",
"ext-json": "*"
@ -15,10 +15,9 @@
}
],
"autoload" : {
"psr-4": {"JsonIterator\\": "src/"}
"psr-4": {"JsonMachine\\": "src/"}
},
"suggest": {
"ext-curl": "To run scripts in src/examples.",
"guzzlehttp/guzzle": "To run scripts in src/examples."
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace JsonIterator\Exception;
namespace JsonMachine\Exception;
class InvalidArgumentException extends \InvalidArgumentException
{

View File

@ -1,6 +1,6 @@
<?php
namespace JsonIterator\Exception;
namespace JsonMachine\Exception;
class PathNotFoundException extends \RuntimeException
{

View File

@ -1,6 +1,6 @@
<?php
namespace JsonIterator\Exception;
namespace JsonMachine\Exception;
class SyntaxError extends \RuntimeException
{

View File

@ -1,11 +1,11 @@
<?php
namespace JsonIterator;
namespace JsonMachine;
use IteratorAggregate;
use JsonIterator\Exception\InvalidArgumentException;
use JsonMachine\Exception\InvalidArgumentException;
class JsonIterator implements IteratorAggregate
class JsonMachine implements IteratorAggregate
{
/**
* @var resource

View File

@ -1,6 +1,6 @@
<?php
namespace JsonIterator;
namespace JsonMachine;
class Lexer implements \IteratorAggregate
{

View File

@ -1,10 +1,10 @@
<?php
namespace JsonIterator;
namespace JsonMachine;
use JsonIterator\Exception\InvalidArgumentException;
use JsonIterator\Exception\PathNotFoundException;
use JsonIterator\Exception\SyntaxError;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\Exception\PathNotFoundException;
use JsonMachine\Exception\SyntaxError;
class Parser implements \IteratorAggregate
{

View File

@ -6,6 +6,6 @@ $client = new \GuzzleHttp\Client();
$respose = $client->request('GET', 'https://httpbin.org/anything?key=value');
// Gets PHP stream resource from Guzzle stream
$phpStream = \GuzzleHttp\Psr7\StreamWrapper::getResource($respose->getBody());
$result = iterator_to_array(\JsonIterator\JsonIterator::fromStream($phpStream));
$result = iterator_to_array(\JsonMachine\JsonMachine::fromStream($phpStream));
assert($result['args'] == ['key' => 'value']);

View File

@ -1,17 +1,17 @@
<?php
namespace JsonIteratorTest;
namespace JsonMachineTest;
use JsonIterator\JsonIterator;
use JsonMachine\JsonMachine;
class JsonIteratorTest extends \PHPUnit_Framework_TestCase
class JsonMachineTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider dataFactories
*/
public function testFactories($methodName, ...$args)
{
$iterator = call_user_func_array(JsonIterator::class."::$methodName", $args);
$iterator = call_user_func_array(JsonMachine::class."::$methodName", $args);
$this->assertSame(["key" => "value"], iterator_to_array($iterator));
}
@ -20,7 +20,7 @@ class JsonIteratorTest extends \PHPUnit_Framework_TestCase
return [
['fromStream', fopen('data://text/plain,{"args": {"key":"value"}}', 'r'), '/args'],
['fromString', '{"args": {"key":"value"}}', '/args'],
['fromFile', __DIR__ . '/JsonIteratorTest.json', '/args'],
['fromFile', __DIR__ . '/JsonMachineTest.json', '/args'],
];
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace JsonIteratorTest;
namespace JsonMachineTest;
use JsonIterator\Lexer;
use JsonIterator\Exception;
use JsonMachine\Lexer;
use JsonMachine\Exception;
class LexerTest extends \PHPUnit_Framework_TestCase
{

View File

@ -1,12 +1,12 @@
<?php
namespace JsonIteratorTest;
namespace JsonMachineTest;
use JsonIterator\Exception\InvalidArgumentException;
use JsonIterator\Exception\PathNotFoundException;
use JsonIterator\Exception\SyntaxError;
use JsonIterator\Lexer;
use JsonIterator\Parser;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\Exception\PathNotFoundException;
use JsonMachine\Exception\SyntaxError;
use JsonMachine\Lexer;
use JsonMachine\Parser;
class ParserTest extends \PHPUnit_Framework_TestCase
{

View File

@ -1,7 +1,7 @@
<?php
use JsonIterator\Lexer;
use JsonIterator\Parser;
use JsonMachine\Lexer;
use JsonMachine\Parser;
require_once __DIR__ . '/../../vendor/autoload.php';