1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 21:18:23 +01:00

README improvements

This commit is contained in:
Filip Halaxa 2020-11-26 20:50:58 +01:00
parent 434cf57f1b
commit 5fcee21b14

View File

@ -16,10 +16,13 @@ JSON Machine is an efficient drop-in replacement of inefficient iteration of big
```diff ```diff
<?php <?php
use \JsonMachine\JsonMachine;
// this often causes Allowed Memory Size Exhausted // this often causes Allowed Memory Size Exhausted
- $users = json_decode(file_get_contents('500MB-users.json')); - $users = json_decode(file_get_contents('500MB-users.json'));
// this usually takes few kB of memory no matter the file size // this usually takes few kB of memory no matter the file size
+ $users = \JsonMachine\JsonMachine::fromFile('500MB-users.json'); + $users = JsonMachine::fromFile('500MB-users.json');
foreach ($users as $id => $user) { foreach ($users as $id => $user) {
// just process $user as usual // just process $user as usual
@ -46,9 +49,9 @@ and uses native `json_decode` to decode JSON document chunks by default. See [cu
## Parsing JSON documents ## Parsing JSON documents
### Simple document ### Simple document
Let's say that `big.json` contains this really big JSON document: Let's say that `fruits.json` contains this really big JSON document:
```json ```json
// big.json // fruits.json
{ {
"apple": { "apple": {
"color": "red" "color": "red"
@ -62,9 +65,11 @@ It can be parsed this way:
```php ```php
<?php <?php
$jsonStream = \JsonMachine\JsonMachine::fromFile('big.json'); use \JsonMachine\JsonMachine;
foreach ($jsonStream as $name => $data) { $fruits = JsonMachine::fromFile('fruits.json');
foreach ($fruits as $name => $data) {
// 1st iteration: $name === "apple" and $data === ["color" => "red"] // 1st iteration: $name === "apple" and $data === ["color" => "red"]
// 2nd iteration: $name === "pear" and $data === ["color" => "yellow"] // 2nd iteration: $name === "pear" and $data === ["color" => "yellow"]
} }
@ -81,15 +86,15 @@ which by default decodes objects - same as `json_decode`
use JsonMachine\JsonDecoder\ExtJsonDecoder; use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonMachine; use JsonMachine\JsonMachine;
$objects = new JsonMachine::fromFile('path/to.json', '', new ExtJsonDecoder); $objects = JsonMachine::fromFile('path/to.json', '', new ExtJsonDecoder);
``` ```
### Parsing a subtree ### Parsing a subtree
If you want to iterate only `fruits-key` subtree in this `fruits.json`: If you want to iterate only `results` subtree in this `fruits.json`:
```json ```json
// fruits.json // fruits.json
{ {
"fruits-key": { "results": {
"apple": { "apple": {
"color": "red" "color": "red"
}, },
@ -103,8 +108,10 @@ do it like this:
```php ```php
<?php <?php
$jsonStream = \JsonMachine\JsonMachine::fromFile("fruits.json", "/fruits-key" /* <- Json Pointer */); use \JsonMachine\JsonMachine;
foreach ($jsonStream as $name => $data) {
$fruits = JsonMachine::fromFile("fruits.json", "/results" /* <- Json Pointer */);
foreach ($fruits 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"]
// 2nd iteration: $name === "pear" and $data === ["color" => "yellow"] // 2nd iteration: $name === "pear" and $data === ["color" => "yellow"]