-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Option to decode JSON as object, instead of associative array #15
Comments
Hello, thanks for your question. This feature is not planned to be implemented. The reason is, that this can be easily done using native PHP type casting. Short versionforeach(JsonMachine::fromFile('some.json') as $array) {
$object = (object) $array; // array casts to stdClass instance, the same as json_decode produces.
echo $object->example;
} That's it. Longer versionIf you want to have your client code clean without having to cast the array every time as in previous example, you can make yourself a little generator function: function toObjects(iterable $items): Generator
{
foreach ($items as $item) {
yield (object) $item;
}
} and use it like this: foreach (toObjects(JsonMachine::fromFile('some.json')) as $object) {
echo $object->example;
} Please let me know if it solves your problem. |
It solves our problem. Thank you for your library. |
Glad to help. If you like it, give it a star, share it and spread the word :). You made a valid point. If you want to be a contributor, you could make pull request with this example in README.md. What do you think? |
I made a function for that called |
Object will be default in version 1.0. See #37. |
|
Done in |
By default,
json_decode
will convert the string to an object, not to associative arrays. I wonder if it's possible to get the same behaviour in json-machine, or should I usejson_decode(json_encode($field), false)
?The text was updated successfully, but these errors were encountered: