GithubHelp home page GithubHelp logo

josephsilber / lazy-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cerbero90/lazy-json

0.0 2.0 0.0 40 KB

๐Ÿผ Framework agnostic package to load heavy JSON into Laravel lazy collections.

License: MIT License

PHP 100.00%

lazy-json's Introduction

๐Ÿผ Lazy JSON

Author PHP Version Laravel Version Octane Compatibility Build Status Coverage Status Quality Score Latest Version Software License PSR-7 PSR-12 Total Downloads

Framework agnostic package to load heavy JSON in lazy collections. Under the hood, the brilliant JSON Machine by @halaxa is used as a lexer and parser.

Need to load paginated items of JSON APIs? Consider using Lazy JSON Pages instead.

Install

In a Laravel application, all you need to do is requiring the package:

composer require cerbero/lazy-json

Otherwise, you also need to register the lazy collection macro manually:

use Cerbero\LazyJson\Macro;
use Illuminate\Support\LazyCollection;

LazyCollection::macro('fromJson', new Macro());

Usage

Loading JSON in lazy collections is possible by using the collection itself or the included helper:

LazyCollection::fromJson($source);

lazyJson($source);

The following are the supported JSON sources:

$source = '{"foo":"bar"}'; // JSON string
$source = ['{"foo":"bar"}']; // any iterable containing JSON, i.e. array or Traversable
$source = 'https://foo.test/endpoint'; // endpoint
$source = Http::get('https://foo.test/endpoint'); // Laravel HTTP client response
$source = '/path/to/file.json'; // JSON file
$source = fopen('/path/to/file.json', 'rb'); // any resource
$source = <Psr\Http\Message\MessageInterface>; // any PSR-7 message, e.g. Guzzle response
$source = <Psr\Http\Message\StreamInterface>; // any PSR-7 stream

Optionally, you can define a dot-noted path to extract only a sub-tree of the JSON. For example, given the following JSON:

{
    "data": [
        {
            "name": "Team 1",
            "users": [
                {
                    "id": 1
                },
                {
                    "id": 2
                }
            ]
        },
        {
            "name": "Team 2",
            "users": [
                {
                    "id": 3
                }
            ]
        }
    ]
}

defining the path data.*.users.*.id would iterate only user IDs:

$ids = lazyJson($source, 'data.*.users.*.id')
    ->filter(fn ($id) => $id % 2 == 0)
    ->all();

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 [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

lazy-json's People

Contributors

cerbero90 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.