GithubHelp home page GithubHelp logo

cerbero90 / json-parser Goto Github PK

View Code? Open in Web Editor NEW
659.0 659.0 10.0 229 KB

๐Ÿงฉ Zero-dependencies lazy parser to read JSON of any dimension and from any source in a memory-efficient way.

License: MIT License

PHP 100.00%
json parser php

json-parser's People

Contributors

cerbero90 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-parser's Issues

Multilevel Item skipping and getting deeply nested objects in memory directly

Detailed description

Getting multiple nested content dynamically.

Context

$json = JsonParser::parse($source)->pointer('/results/-/gender');

foreach ($json as $key => $value) {
    // 1st iteration: $key === 'gender', $value === 'female'
    // 2nd iteration: $key === 'gender', $value === 'female'
    // 3rd iteration: $key === 'gender', $value === 'male'
    // and so on for all the objects in the array...
}

Same like above example we can skip any element keys after results, but we cant do that on multiple level

- $json = JsonParser::parse($source)->pointer('/results/-/gender');
+ $json = JsonParser::parse($source)->pointer('/results/-/-/gender');

Error when parsing from iterable (array)

I get a TypeError when I try to use the JsonParser on an array. The error happens on line 59 in \Cerbero\JsonParser\Tokens\Lexer because the Lexer assumes the $chunk variable will contain a string and calls strlen() upon it, however when parsing from a nested array the $chunk variable contains an array as well.

Basically, I think this is a bug. I'm open to the option that I'm doing something wrong, but I couldn't figure out what it could be and it feels like a bug. Or did I get all of this completely wrong and the iterable source should contain an array of JSON strings? ๐Ÿ˜…

Here is a chunk of code that should produce the bug I'm describing:

$data = [
    "pre" => [
        "locations" => [
            "locations" => [
                [
                    "id" => 2333,
                    "title" => "Hollywood",
                    "created_at" => "2022-10-02 10:17:12",
                    "updated_at" => "2022-10-02 10:17:12",
                    "deleted_at" => null
                ]
            ]
        ],
        "categories" => [
            "categories" => [
                [
                    "id" => 1302,
                    "business_id" => 186,
                    "title" => "Drama",
                    "deleted_at" => null,
                    "created_at" => "2019-02-10 06:28:28",
                    "updated_at" => "2020-02-08 05:16:52"
                ]
            ]
        ]
    ],
    "products" => [
        [
            "id" => 4465,
            "backoffice_title" => "Acting class",
            "created_at" => "2017-02-22 16:20:19",
            "updated_at" => "2023-06-30 10:25:59",
            "deleted_at" => null
        ]
    ],
    "post" => [
        "timeslots-4465" => [
            "timeslots" => [
                [
                    "id" => 250334,
                    "product_id" => 4465,
                    "created_at" => "2019-02-10 06:28:29",
                    "updated_at" => "2021-09-03 15:03:27"
                ]
            ]
        ]
    ]
];
        
(new JsonParser($data))->pointer('/pre/locations', fn ($data) => var_dump($data))->traverse();

Possible implementation

I'm guessing the iterable source should not be processed by the Lexer at all

Your environment

  • Version used: 1.0.0
  • PHP version: 8.2.7
  • Ran on Laravel Sail 1.7 with Laravel 8.83.27

Performance?

Detailed description

After using this library to iterate larger JSON responses from Guzzle (using the pointer /- on \Psr\Http\Message\StreamInterface), I noticed a significant reduction in memory usage (great!), but as well a significant increase in CPU load.
Is this expected?
What are possible mitigation strategies? Would different options (chunk size, lazy pointers, ..) help?

Context

Of course generally the "space-time tradeoff" implies some increase in CPU, but I expected this to be not significant..

Possible implementation

Possibly a section on this in the readme would help, maybe even a comparison to the alternatives to see if this is on the same level?

Your environment

  • Version used: PHP 8.1
  • Operating system and version: Ubuntu 22.0

Examples for each source

It would be great if there were examples for each source. Since now it is not clear, for example, how to work with Laravel request, when you receive data via the API, the request immediately produces an array and because of this an error.

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.