Framework agnostic package to load heavy JSON in lazy collections.

Overview

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 lexer and parser.

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:

; // any PSR-7 message, e.g. Guzzle response $source = ; // any PSR-7 stream ">
$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.

You might also like...
PHPR or PHP Array Framework is a framework highly dependent to an array structure.

this is new repository for php-framework Introduction PHPR or PHP Array Framework is a framework highly dependent to an array structure. PHPR Framewor

I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

Framework X – the simple and fast micro framework for building reactive web applications that run anywhere.

Framework X Framework X – the simple and fast micro framework for building reactive web applications that run anywhere. Quickstart Documentation Tests

Framework X is a simple and fast micro framework based on PHP
Framework X is a simple and fast micro framework based on PHP

Framework X is a simple and fast micro framework based on PHP. I've created a simple CRUD application to understand how it works. I used twig and I created a custom middleware to handle PUT, DELETE methods.

Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components
Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components

Spiral HTTP Application Skeleton Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components.

Sunhill Framework is a simple, fast, and powerful PHP App Development Framework

Sunhill Framework is a simple, fast, and powerful PHP App Development Framework that enables you to develop more modern applications by using MVC (Model - View - Controller) pattern.

A PHP framework for web artisans.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

The Symfony PHP framework

Symfony is a PHP framework for web and console applications and a set of reusable PHP components. Symfony is used by thousands of web applications (in

Open Source PHP Framework (originally from EllisLab)

What is CodeIgniter CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable

Comments
  • Update `halaxa/json-machine` dependency to `^0.8|^1.0`

    Update `halaxa/json-machine` dependency to `^0.8|^1.0`

    Description

    This PR removes support for the relatively old 0.7.* releases of halaxa/json-machine and adds support for 0.8.* and 1.*

    Motivation and context

    composer outdated shows halaxa/json-machine as an outdated transitive dependency in some of my projects (although it works just fine with the 0.7.* releases).

    How has this been tested?

    The 0.8.* releases should be covered by the --prefer-lowest flags, 1.* with the other matrix tests.

    I tried adding PHP 8.1 and Laravel 9 to the test matrix, but this resulted in ~140 runs and didn't feel right 😅

    Types of changes

    I don't think this is a breaking change - if a project or one of its other dependencies requires "halaxa/json-machine": "^0.7", it will just stay on lazy-json 1.1.1.

    The change in the GitHub action just prevents forks like mine from trying to upload the code coverage reports to Scrutinizer. I hope it's ok that I included the change in this PR, otherwise I'll revert it and create a new PR with the change.

    Checklist:

    • [x] I have read the CONTRIBUTING document.
    • [x] My pull request addresses exactly one patch/feature.
    • [x] I have created a branch for this patch/feature.
    • [x] Each individual commit in the pull request is meaningful.
    • [x] I have adapted the tests to cover my changes.
    • [x] If my change requires a change to the documentation, I have updated it accordingly.
    opened by jeromegamez 3
Owner
Andrea Marco Sartori
Andrea Marco Sartori
This package has framework agnostic Cross-Origin Resource Sharing (CORS) implementation.

Description This package has framework agnostic Cross-Origin Resource Sharing (CORS) implementation. It is complaint with PSR-7 HTTP message interface

null 60 Nov 9, 2022
A framework agnostic, developer friendly wrapper around Fractal

A developer friendly wrapper around Fractal Fractal is an amazing package to transform data before using it in an API. Unfortunately working with Frac

Spatie 356 Dec 29, 2022
PPM is a process manager, supercharger and load balancer for modern PHP applications.

PPM - PHP Process Manager PHP-PM is a process manager, supercharger and load balancer for PHP applications. It's based on ReactPHP and works best with

PPM - PHP Process Manager 6.5k Dec 27, 2022
Not really a load balancer.

302 This is a load balancer that doesn't act as a load balancer. It is a redirect balancer. Instead of forwarding traffic to a specific host/port, it

52 Entertainment 10 Mar 16, 2022
💾 High-performance PHP application server, load-balancer and process manager written in Golang. RR2 releases repository.

RoadRunner is an open-source (MIT licensed) high-performance PHP application server, load balancer, and process manager. It supports running as a serv

Spiral Scout 45 Nov 29, 2022
🤯 High-performance PHP application server, load-balancer and process manager written in Golang

RoadRunner is an open-source (MIT licensed) high-performance PHP application server, load balancer, and process manager. It supports running as a serv

Spiral Scout 6.9k Jan 3, 2023
This JSON marshaller is based on the one built into FEAST framework

Standalone JSON marshaller based off the one built into FEAST framework at feast/framework on packagist or feastframework/framework on github

null 13 Dec 9, 2022
An extension to the SLIM framework to implement json API's with great ease.

slim-jsonAPI This is an extension to the SLIM framework to implement json API's with great ease. Installation Using composer you can add use this as y

Jonathan Tavares 269 Jul 23, 2022
CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework

CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework. It is free, Open Source and is distributed under Free Public Lice

Nazar Mokrynskyi 150 Apr 12, 2022
I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

null 14 Aug 14, 2022