Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] toArray is a bad and wrong name for json #32481

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

Closed
zorn-v opened this issue Jul 10, 2019 · 9 comments
Closed

[HttpClient] toArray is a bad and wrong name for json #32481

zorn-v opened this issue Jul 10, 2019 · 9 comments

Comments

@zorn-v
Copy link

zorn-v commented Jul 10, 2019

Imagine that I got true response from API. That method do it right and return PHP true (coz json_decode). But... TO ARRAY ? Is true IS ARRAY ? Or maybe 123 is array also ? πŸ˜„

Also someone want to get PHP object instead array (preferences).

Maybe one more method like getJson($throw = true, $asArray = true) and deprecate toArray ? 😸

@ro0NL
Copy link
Contributor

ro0NL commented Jul 10, 2019

See #31913 and #30499 (comment) for some more info

(TLDR; it's about "array response bodies", not JSON per se)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 10, 2019

JSON stands for JavaScript Object Notation. No API should respond with anything else than an object and/or an array. Yes, this is a should. toArray works with conforming APIs, and will throw a well-defined exception when something else is returned. When this happens, one should use getContent() instead.
The links @ro0NL shared are relevant to get a better understanding of why/how things are designed this way.

@javiereguiluz
Copy link
Member

Also, we're adding more ->to...() methods, such as ->toStream() added in #32290.

@zorn-v
Copy link
Author

zorn-v commented Jul 10, 2019

Maybe getJson is bad name also, but better then toArray for method that can return NOT ARRAY.
e.g var_dump(json_decode('true', true));

@zorn-v
Copy link
Author

zorn-v commented Jul 10, 2019

I'm about naming. not else. (but parameter for return object will be fine too πŸ˜„ )

@ro0NL
Copy link
Contributor

ro0NL commented Jul 10, 2019

but better then toArray for method that can return NOT ARRAY.

it will always return an array, or error as such. That's exactly the main-feature of this method.

@zorn-v
Copy link
Author

zorn-v commented Jul 10, 2019

Nope. It just do json_decode on ANY DATA

$content = json_decode($content, true, 512, JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0));

Data can be simple "json encoded" string for example. Like "example"

@ro0NL
Copy link
Contributor

ro0NL commented Jul 10, 2019

see a few lines below :)

if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, %s returned.', \gettype($content)));
}

@zorn-v
Copy link
Author

zorn-v commented Jul 10, 2019

Oooops, missed it )

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

No branches or pull requests

5 participants