cerbero90 / json-parser Goto Github PK
View Code? Open in Web Editor NEW๐งฉ Zero-dependencies lazy parser to read JSON of any dimension and from any source in a memory-efficient way.
License: MIT License
๐งฉ Zero-dependencies lazy parser to read JSON of any dimension and from any source in a memory-efficient way.
License: MIT License
There is a already a package with 0 dependencies and over 3 million downloads doing more or less the same. How is this package different from it?
Getting multiple nested content dynamically.
$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');
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.
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?
Of course generally the "space-time tradeoff" implies some increase in CPU, but I expected this to be not significant..
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?
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();
I'm guessing the iterable source should not be processed by the Lexer at all
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.