Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last Item Ignored #64

Open
baoqiangy opened this issue Mar 18, 2018 · 4 comments
Open

Last Item Ignored #64

baoqiangy opened this issue Mar 18, 2018 · 4 comments

Comments

@baoqiangy
Copy link

I tested a small file with 4 elements in there. I noticed that the last element is always ignored. Is it because you code return an empty element as the first one and by the time the last element is read, the parser simply thinks it is done?

@gonzofy
Copy link
Contributor

gonzofy commented Mar 18, 2018

@baoqiangy That's not a problem I've ever had with the parser. Without knowing the specifics, I suspect it's a mistake in your Listener implementation.

@gonzofy gonzofy closed this as completed Mar 18, 2018
@baoqiangy
Copy link
Author

baoqiangy commented Mar 18, 2018

Here is the modified code of the example_geojson.php, I only changed the file to use small_json.json and print the json every time a json is recognized.

`<?php

require_once '/../vendor/autoload.php';

$testfile = 'small_json.json';

$listener = new \JsonStreamingParser\Listener\GeoJsonListener(function ($item) {
print_r($item);
});
$stream = fopen($testfile, 'r');
try {
$parser = new \JsonStreamingParser\Parser($stream, $listener);
$parser->parse();
fclose($stream);
} catch (Exception $e) {
fclose($stream);
throw $e;
}`

Here is the json file I used (small_json.json).

[ { "id": 707860, "name": "Hurzuf", "country": "UA", "coord": { "lon": 34.283333, "lat": 44.549999 } }, { "id": 519188, "name": "Novinki", "country": "RU", "coord": { "lon": 37.666668, "lat": 55.683334 } }, { "id": 1283378, "name": "Gorkhā", "country": "NP", "coord": { "lon": 84.633331, "lat": 28 } }, { "id": 1270260, "name": "State of Haryāna", "country": "IN", "coord": { "lon": 76, "lat": 29 } } ]

Here is the output when I execute the example_test.php in a browser on localhost.
`Array ( [id] => 707860 [name] => Hurzuf [country] => UA [coord] => Array ( [lon] => 34.283333 [lat] => 44.549999 ) )
Array ( [id] => 519188 [name] => Novinki [country] => RU [coord] => Array ( [lon] => 37.666668 [lat] => 55.683334 ) )
Array ( [id] => 1283378 [name] => Gorkhā [country] => NP [coord] => Array ( [lon] => 84.633331 [lat] => 28 ) )

`

Apparently, the last one is not printed. I could not figure out what caused this.

@gonzofy
Copy link
Contributor

gonzofy commented Mar 18, 2018

Got it. I suspect it's a problem with the logic in the GeoJsonListener class.

Specifically, this logic:

        // Call the callback when returning to the second level
        if ($this->level == 2 && is_callable($this->callback)) {
            call_user_func($this->callback, $this->json);
        }

Since the last element doesn't return to level 2. I don't use the GeoJsonListener myself; the fix looks easy enough, so if you figure it out and submit a pull request I'd be happy to merge.

@gonzofy gonzofy reopened this Mar 18, 2018
@sg3w
Copy link

sg3w commented Jul 2, 2020

I`ve got the same Problem and the same Node has many hits. The Return-Level is 1.

if (1 === $this->level && \is_callable($this->callback)) { \call_user_func($this->callback, $this->json); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants