|
| 1 | +# JSON Objects |
| 2 | + |
| 3 | +[![Author][ico-author]][link-author] |
| 4 | +[![PHP Version][ico-php]][link-php] |
| 5 | +[![Build Status][ico-actions]][link-actions] |
| 6 | +[![Coverage Status][ico-scrutinizer]][link-scrutinizer] |
| 7 | +[![Quality Score][ico-code-quality]][link-code-quality] |
| 8 | +[![Latest Version][ico-version]][link-packagist] |
| 9 | +[![Software License][ico-license]](LICENSE.md) |
| 10 | +[![PSR-7][ico-psr7]][link-psr7] |
| 11 | +[![PSR-12][ico-psr12]][link-psr12] |
| 12 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 13 | + |
| 14 | +This package extracts JSON objects from large JSON sources like files, endpoints and streams while saving memory. It parses heavy JSONs by using [JsonStreamingParser][link-jsonstreamingparser] and provides an easy API to declare what objects to extract and process. |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +Via Composer |
| 19 | + |
| 20 | +``` bash |
| 21 | +composer require cerbero/json-objects |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Simply pass the JSON source (files, endpoints or streams) and optionally the key where objects are contained to create a new instance of `JsonObjects`. You can also call the factory method `from()`: |
| 27 | + |
| 28 | +``` php |
| 29 | +$source = 'https://jsonplaceholder.typicode.com/users'; |
| 30 | + |
| 31 | +// Create a new instance specifying the JSON source to extract objects from |
| 32 | +new JsonObjects($source); |
| 33 | +// or |
| 34 | +JsonObjects::from($source); |
| 35 | + |
| 36 | +// Create a new instance specifying the JSON source and the key to extract objects from |
| 37 | +new JsonObjects($source, 'address.geo'); |
| 38 | +// or |
| 39 | +JsonObjects::from($source, 'address.geo'); |
| 40 | +``` |
| 41 | + |
| 42 | +When providing a key to extract objects from, you can use the dot notation to indicate nested sections of a JSON. For example `nested.*.key` extracts all the objects in the property `key` of every object contained in `nested`. |
| 43 | + |
| 44 | +Under the hood `JsonObjects` supports PSR-7, hence any implementation of [MessageInterface][link-message-interface] or [StreamInterface][link-stream-interface] is a valid source. This makes interactions with other packages supporting PSR-7 (e.g. Guzzle) even more convenient: |
| 45 | + |
| 46 | +``` php |
| 47 | +$response = $guzzle->get('https://jsonplaceholder.typicode.com/users'); |
| 48 | + |
| 49 | +// Create a new instance by passing an implementation of MessageInterface |
| 50 | +JsonObjects::from($response); |
| 51 | + |
| 52 | +// Create a new instance by passing an implementation of StreamInterface |
| 53 | +JsonObjects::from($response->getBody()); |
| 54 | +``` |
| 55 | + |
| 56 | +Finally you can decide whether to extract and process objects one by one or in chunks. The memory will be allocated to read only these objects instead of the whole JSON document: |
| 57 | + |
| 58 | +``` php |
| 59 | +// Extract and process one object at a time from the given JSON source |
| 60 | +JsonObjects::from($source)->each(function (array $object) { |
| 61 | + // Process one object |
| 62 | +}); |
| 63 | + |
| 64 | +// Extract and process a chunk of objects at a time from the given JSON source |
| 65 | +JsonObjects::from($source)->chunk(100, function (array $objects) { |
| 66 | + // Process 100 objects |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +## Change log |
| 71 | + |
| 72 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 73 | + |
| 74 | +## Testing |
| 75 | + |
| 76 | +``` bash |
| 77 | +$ composer test |
| 78 | +``` |
| 79 | + |
| 80 | +## Contributing |
| 81 | + |
| 82 | +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details. |
| 83 | + |
| 84 | +## Security |
| 85 | + |
| 86 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 87 | + |
| 88 | +## Credits |
| 89 | + |
| 90 | +- [Andrea Marco Sartori][link-author] |
| 91 | +- [JsonStreamingParser][link-jsonstreamingparser] |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 96 | + |
| 97 | +[ico-author]: https://img.shields.io/static/v1?label=author&message=cerbero90&color=50ABF1&logo=twitter&style=flat-square |
| 98 | +[ico-php]: https://img.shields.io/packagist/php-v/cerbero/json-objects?color=%234F5B93&logo=php&style=flat-square |
| 99 | +[ico-version]: https://img.shields.io/packagist/v/cerbero/json-objects.svg?label=version&style=flat-square |
| 100 | +[ico-actions]: https://img.shields.io/github/workflow/status/cerbero90/json-objects/build?style=flat-square&logo=github |
| 101 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 102 | +[ico-psr7]: https://img.shields.io/static/v1?label=compliance&message=PSR-7&color=blue&style=flat-square |
| 103 | +[ico-psr12]: https://img.shields.io/static/v1?label=compliance&message=PSR-12&color=blue&style=flat-square |
| 104 | +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/cerbero90/json-objects.svg?style=flat-square&logo=scrutinizer |
| 105 | +[ico-code-quality]: https://img.shields.io/scrutinizer/g/cerbero90/json-objects.svg?style=flat-square&logo=scrutinizer |
| 106 | +[ico-downloads]: https://img.shields.io/packagist/dt/cerbero/json-objects.svg?style=flat-square |
| 107 | + |
| 108 | +[link-author]: https://twitter.com/cerbero90 |
| 109 | +[link-php]: https://www.php.net |
| 110 | +[link-packagist]: https://packagist.org/packages/cerbero/json-objects |
| 111 | +[link-actions]: https://github.com/cerbero90/json-objects/actions?query=workflow%3Abuild |
| 112 | +[link-psr7]: https://www.php-fig.org/psr/psr-7/ |
| 113 | +[link-psr12]: https://www.php-fig.org/psr/psr-12/ |
| 114 | +[link-scrutinizer]: https://scrutinizer-ci.com/g/cerbero90/json-objects/code-structure |
| 115 | +[link-code-quality]: https://scrutinizer-ci.com/g/cerbero90/json-objects |
| 116 | +[link-downloads]: https://packagist.org/packages/cerbero/json-objects |
| 117 | +[link-jsonstreamingparser]: https://github.com/salsify/jsonstreamingparser |
| 118 | +[link-message-interface]: https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php |
| 119 | +[link-stream-interface]: https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php |
| 120 | +[link-contributors]: ../../contributors |
0 commit comments