mirror of
https://github.com/halaxa/json-machine.git
synced 2025-01-17 13:08:16 +01:00
Renamed to JsonMachine
This commit is contained in:
parent
132c49bb68
commit
a4143d9e61
23
README.md
23
README.md
@ -1,12 +1,13 @@
|
|||||||
# JSON Iterator
|
# JSON Machine
|
||||||
|
|
||||||
Json Iterator is a simple **JSON stream parser for PHP** based on coroutines,
|
Json Machine is a Fast, efficient and easy-to-use JSON stream parser based on coroutines
|
||||||
developed for extremely large JSON datasets. Main features are:
|
developed for unpredictably long JSON streams or documents. 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 JSON of any size with `foreach`. No events and callbacks.
|
||||||
- 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)
|
|
||||||
- Constant memory footprint for unpredictably large JSON documents.
|
- 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
|
## Examples
|
||||||
### Parsing simple JSON document
|
### Parsing simple JSON document
|
||||||
@ -26,7 +27,7 @@ It can be parsed this way:
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$jsonStream = \JsonIterator\JsonIterator::fromFile('big.json');
|
$jsonStream = \JsonMachine\JsonMachine::fromFile('big.json');
|
||||||
|
|
||||||
foreach ($jsonStream as $name => $data) {
|
foreach ($jsonStream as $name => $data) {
|
||||||
// 1st iteration: $name === "apple" and $data === ["color" => "red"]
|
// 1st iteration: $name === "apple" and $data === ["color" => "red"]
|
||||||
@ -56,7 +57,7 @@ do it like this:
|
|||||||
```php
|
```php
|
||||||
<?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) {
|
foreach ($jsonStream as $name => $data) {
|
||||||
// The same as above, which means:
|
// The same as above, which means:
|
||||||
// 1st iteration: $name === "apple" and $data === ["color" => "red"]
|
// 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
|
> 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
|
> `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.
|
> you are currently iterating. Thus the memory consumption is constant.
|
||||||
## Parsing API responses
|
## Parsing API responses
|
||||||
If you use this library to parse large API responses, all you need to do is passing the stream resource
|
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
|
### GuzzleHttp example
|
||||||
Guzzle uses its own streams, but they can be converted back to PHP streams by calling
|
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)
|
`\GuzzleHttp\Psr7\StreamWrapper::getResource()`. See [GuzzleHttp example](src/examples/guzzleHttp.php)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "jsonator/jsonator",
|
"name": "halaxa/json-machine",
|
||||||
"description": "Json parser consuming a stream of json data with easy, iterable api.",
|
"description": "Fast, efficient and easy-to-use JSON stream parser developed for unpredictably long JSON streams or documents.",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.6",
|
"php": ">=5.6",
|
||||||
"ext-json": "*"
|
"ext-json": "*"
|
||||||
@ -15,10 +15,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload" : {
|
"autoload" : {
|
||||||
"psr-4": {"JsonIterator\\": "src/"}
|
"psr-4": {"JsonMachine\\": "src/"}
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-curl": "To run scripts in src/examples.",
|
|
||||||
"guzzlehttp/guzzle": "To run scripts in src/examples."
|
"guzzlehttp/guzzle": "To run scripts in src/examples."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator\Exception;
|
namespace JsonMachine\Exception;
|
||||||
|
|
||||||
class InvalidArgumentException extends \InvalidArgumentException
|
class InvalidArgumentException extends \InvalidArgumentException
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator\Exception;
|
namespace JsonMachine\Exception;
|
||||||
|
|
||||||
class PathNotFoundException extends \RuntimeException
|
class PathNotFoundException extends \RuntimeException
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator\Exception;
|
namespace JsonMachine\Exception;
|
||||||
|
|
||||||
class SyntaxError extends \RuntimeException
|
class SyntaxError extends \RuntimeException
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator;
|
namespace JsonMachine;
|
||||||
|
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
use JsonIterator\Exception\InvalidArgumentException;
|
use JsonMachine\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class JsonIterator implements IteratorAggregate
|
class JsonMachine implements IteratorAggregate
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var resource
|
* @var resource
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator;
|
namespace JsonMachine;
|
||||||
|
|
||||||
class Lexer implements \IteratorAggregate
|
class Lexer implements \IteratorAggregate
|
||||||
{
|
{
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIterator;
|
namespace JsonMachine;
|
||||||
|
|
||||||
use JsonIterator\Exception\InvalidArgumentException;
|
use JsonMachine\Exception\InvalidArgumentException;
|
||||||
use JsonIterator\Exception\PathNotFoundException;
|
use JsonMachine\Exception\PathNotFoundException;
|
||||||
use JsonIterator\Exception\SyntaxError;
|
use JsonMachine\Exception\SyntaxError;
|
||||||
|
|
||||||
class Parser implements \IteratorAggregate
|
class Parser implements \IteratorAggregate
|
||||||
{
|
{
|
||||||
|
@ -6,6 +6,6 @@ $client = new \GuzzleHttp\Client();
|
|||||||
$respose = $client->request('GET', 'https://httpbin.org/anything?key=value');
|
$respose = $client->request('GET', 'https://httpbin.org/anything?key=value');
|
||||||
// Gets PHP stream resource from Guzzle stream
|
// Gets PHP stream resource from Guzzle stream
|
||||||
$phpStream = \GuzzleHttp\Psr7\StreamWrapper::getResource($respose->getBody());
|
$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']);
|
assert($result['args'] == ['key' => 'value']);
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?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
|
* @dataProvider dataFactories
|
||||||
*/
|
*/
|
||||||
public function testFactories($methodName, ...$args)
|
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));
|
$this->assertSame(["key" => "value"], iterator_to_array($iterator));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ class JsonIteratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
return [
|
return [
|
||||||
['fromStream', fopen('data://text/plain,{"args": {"key":"value"}}', 'r'), '/args'],
|
['fromStream', fopen('data://text/plain,{"args": {"key":"value"}}', 'r'), '/args'],
|
||||||
['fromString', '{"args": {"key":"value"}}', '/args'],
|
['fromString', '{"args": {"key":"value"}}', '/args'],
|
||||||
['fromFile', __DIR__ . '/JsonIteratorTest.json', '/args'],
|
['fromFile', __DIR__ . '/JsonMachineTest.json', '/args'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIteratorTest;
|
namespace JsonMachineTest;
|
||||||
|
|
||||||
use JsonIterator\Lexer;
|
use JsonMachine\Lexer;
|
||||||
use JsonIterator\Exception;
|
use JsonMachine\Exception;
|
||||||
|
|
||||||
class LexerTest extends \PHPUnit_Framework_TestCase
|
class LexerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace JsonIteratorTest;
|
namespace JsonMachineTest;
|
||||||
|
|
||||||
use JsonIterator\Exception\InvalidArgumentException;
|
use JsonMachine\Exception\InvalidArgumentException;
|
||||||
use JsonIterator\Exception\PathNotFoundException;
|
use JsonMachine\Exception\PathNotFoundException;
|
||||||
use JsonIterator\Exception\SyntaxError;
|
use JsonMachine\Exception\SyntaxError;
|
||||||
use JsonIterator\Lexer;
|
use JsonMachine\Lexer;
|
||||||
use JsonIterator\Parser;
|
use JsonMachine\Parser;
|
||||||
|
|
||||||
class ParserTest extends \PHPUnit_Framework_TestCase
|
class ParserTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use JsonIterator\Lexer;
|
use JsonMachine\Lexer;
|
||||||
use JsonIterator\Parser;
|
use JsonMachine\Parser;
|
||||||
|
|
||||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user