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

Change default decoding structure from array to object #37

Closed
halaxa opened this issue Nov 10, 2020 · 6 comments
Closed

Change default decoding structure from array to object #37

halaxa opened this issue Nov 10, 2020 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@halaxa
Copy link
Owner

halaxa commented Nov 10, 2020

This will make it more predictable as json_decode works the same way.
The only thing needed to do is to change the line with default instantiation of ExtJsonDecoder in Parser.

Huge BC break - will wait to version 1.0

@halaxa halaxa added this to the 1.0.0 milestone Nov 10, 2020
@halaxa halaxa changed the title Change default decoding structure to object Change default decoding structure from array to object Nov 10, 2020
@dennisong45
Copy link

Sorry to hijack your thread, BY default JSONMachine FROM File return Object right now.

How do i return as Array?

$x = JsonMachine::fromFile('myjson');
echo(gettype($x) . PHP_EOL);

object

@halaxa
Copy link
Owner Author

halaxa commented Apr 1, 2021

The $x in your example is of type object because it is a lazy Iterator. When you start iterating it in foreach, the individual items will be arrays. It will not produce arrays until you start iterating the object.

$x = JsonMachine::fromFile('myjson');
foreach ($x as $item) {
    echo(gettype($item)); // array
}

@dennisong45
Copy link

dennisong45 commented Apr 1, 2021

I see thank you.
i'm still having trouble

Let say for example i have JSON with the following format.

{ "sections":{ "2d4":{ "id":"2d4", "n":"MY_SECTION", "ls":{ "0":{ "n":"v1" }, "1":{ "n":"v2" } }, "ix":107 } }, "mb_index":528 }

foreach ($x as $sections_info) {
	
  	foreach($sections_info["sections"] as $k => $v){
	  	echo $k;
  	}
}
PHP Notice:  Undefined index: sections in jsonmachine.php on line 24

@dennisong45
Copy link

Is there a way to Return it as an Array instead of an object directly? I try converting it after reading it. The data malformed.

@halaxa
Copy link
Owner Author

halaxa commented Apr 2, 2021

Can you please post your question to Github Discussions to Q&A category? New topic - Q&A. Let's continue there. Thank you.

@halaxa halaxa added the enhancement New feature or request label Oct 24, 2021
@halaxa
Copy link
Owner Author

halaxa commented Dec 21, 2021

This will make it more predictable as json_decode works the same way. The only thing needed to do is to change the line with default instantiation of ExtJsonDecoder in Parser.

Huge BC break - will wait to version 1.0

Done in master branch.

@halaxa halaxa closed this as completed Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants