mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-17 13:08:16 +01:00
6.8 KiB
6.8 KiB
🧩 JSON Parser
Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.
📦 Install
Via Composer:
composer require cerbero/json-parser
🔮 Usage
JSON Parser provides a minimal API to read large JSON from any source:
use Cerbero\JsonParser\JsonParser;
// the JSON source in this example is an API endpoint
$source = 'https://randomuser.me/api/1.4?seed=json-parser&results=5';
foreach (new JsonParser($source) as $key => $value) {
// instead of loading the whole JSON, we keep in memory only one key and value at a time
}
Depending on our taste, we can instantiate the parser in 3 different ways:
use Cerbero\JsonParser\JsonParser;
// classic object instantiation
new JsonParser($source);
// static instantiation, facilitates methods chaining
JsonParser::parse($source);
// namespaced function
use function Cerbero\JsonParser\parseJson;
parseJson($source);
Sources
A wide range of JSON sources is supported, here is the full list:
- strings, e.g.
{"foo":"bar"}
- iterables, i.e. arrays or instances of
Traversable
- files, e.g.
/path/to/large_file.json
- resources, e.g. streams
- API endpoint URLs, e.g.
https://endpoint.json
or any instance ofPsr\Http\Message\UriInterface
- PSR-7 compliant requests, i.e. any instance of
Psr\Http\Message\RequestInterface
- PSR-7 compliant messages, i.e. any instance of
Psr\Http\Message\MessageInterface
- PSR-7 compliant streams, i.e. any instance of
Psr\Http\Message\StreamInterface
- responses from the Laravel HTTP client, i.e. any instance of
Illuminate\Http\Client\Response
- user-defined sources, i.e. any instance of
Cerbero\JsonParser\Sources\Source
📆 Change log
Please see CHANGELOG for more information on what has changed recently.
🧪 Testing
composer test
💞 Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
🧯 Security
If you discover any security related issues, please email andrea.marco.sartori@gmail.com instead of using the issue tracker.
🏅 Credits
⚖️ License
The MIT License (MIT). Please see License File for more information.