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

TO_JSON for Mojo::Collection #801

Merged
merged 4 commits into from May 27, 2015
Merged

TO_JSON for Mojo::Collection #801

merged 4 commits into from May 27, 2015

Conversation

wttw
Copy link
Contributor

@wttw wttw commented May 26, 2015

Adds Mojo::Collection::TO_JSON

The name is slightly misleading as it doesn't convert to JSON, rather it returns an array reference in the same way as to_array. This lets Mojo::JSON and friends convert a Mojo::Collection to a JSON array.

Amongst other things it reduces noise when returning Mojo::Pg result sets as JSON:

$c->render(json => {foo => $results->hashes});

vs

$c->render(json => {foo => $results->hashes->to_array});

@@ -90,6 +90,8 @@ sub tap { shift->Mojo::Base::tap(@_) }

sub to_array { [@{shift()}] }

sub TO_JSON { [@{shift()}] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upper case methods should be at the top.

@kraih
Copy link
Member

kraih commented May 26, 2015

👎 from me i'm afraid. Like i said on IRC before, we've had a lot of bad luck with this kind of magic in Mojo::Collection, which makes me very sceptical of any new additions. There is also an inconsistency between Mojo::ByteStream and Mojo::Collection now, since both would behave differently when passed to JSON encoders like Cpanel::JSON::XS.

@kraih
Copy link
Member

kraih commented May 26, 2015

Just in case some of you don't remember those magical mistakes... we've had Mojo::Collection return true or false in boolean context depending on the number of elements, stringify all elements joined with newlines, and allowed arbitrary methods to be called on all elements with AUTOLOAD. And every single one of those turned out to be a huge problem.

@Grinnz
Copy link
Contributor

Grinnz commented May 26, 2015

I would be +1 if accompanied by a matching method for Mojo::ByteStream, and with the docs fixed up. I don't believe this is a problem like the other Mojo::Collection issues, as this only affects JSON encoding unlike overloading and AUTOLOAD, and treats the object as an arrayref and not an array.

@tempire
Copy link
Contributor

tempire commented May 26, 2015

It doesn't seem so bad to me, given that the "magic" is only what Mojo::JSON already does. I'm 👍, but open to being convinced otherwise.

@@ -322,6 +324,12 @@ Alias for L<Mojo::Base/"tap">.

Turn collection into array reference.

=head2 TO_JSON
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section needs to be moved to the top too.

@marcusramberg
Copy link
Member

I'm 👎 for now because of the inconsistency it adds, I'm not as worried about the "magic"/convention of TO_JSON tho. I might be inclined to change my vote if Mojo::ByteStream got the TO_JSON method too.

@jhthorsen
Copy link
Member

👍 from me. Same as @Grinnz's comments. I would actually stretch it to considering adding TO_JSON() to all the modules that does to_array or to_string, to make other mojo-objects consistent with Cpanel::JSON::XS.

I like throwing by nested objects on to Mojo::JSON and just let it figure out what to do, instead of traversing the objects twice to make sure all the array-ref-like objects are array-refs first.

@jberger
Copy link
Member

jberger commented May 27, 2015

Like @jhthorsen I think that many of the classes that implement to_string (and to_array, but there's only one of those) ought to have TO_JSON, except that I don't think they all should. Here is the list of those:

$ ack -l 'sub to_string'
lib/Mojo/ByteStream.pm
lib/Mojo/Cookie/Request.pm
lib/Mojo/Cookie/Response.pm
lib/Mojo/Cookie.pm
lib/Mojo/Date.pm
lib/Mojo/DOM.pm
lib/Mojo/Exception.pm
lib/Mojo/Headers.pm
lib/Mojo/Home.pm
lib/Mojo/Message.pm
lib/Mojo/Parameters.pm
lib/Mojo/Path.pm
lib/Mojo/URL.pm
lib/Mojolicious/Routes/Route.pm

While I can see the need to serialize several of these, I don't think we need to really concern ourselves with all of them (would someone really want to serialize a route?). That said, I'm now choosing between consistency and practicality, which I know will displease @kraih. I still remain neutral I suppose, though with some amount of sadness.

@kraih
Copy link
Member

kraih commented May 27, 2015

I have to strongly disagree with @jhthorsen, adding a TO_JSON method to classes like Mojo::Message would be a huge mistake.

@jhthorsen
Copy link
Member

Sorry! Let me rephrase. I meant to make it consistent between Mojo::JSON and Cpanel::JSON::XS, I would add TO_JSON() to all the classes which does string overloading. I guess my to_array() statement still stand, since only Mojo::Collection has that method.

$ ack -l "to_string" lib/ | xargs ack -l '""' | xargs ack -h "^package"
package Mojo::Path;
package Mojo::Date;
package Mojo::Exception;
package Mojo::DOM;
package Mojo::ByteStream;
package Mojo::Parameters;
package Mojo::Cookie;
package Mojo::Home;
package Mojo::URL;

@kraih
Copy link
Member

kraih commented May 27, 2015

Ok, after some more discussions on IRC, i don't think this proposal is ready for voting yet. We first need to decide if we want Mojo::JSON to behave consistently (or to what degree) with Cpanel::JSON::XS.

@jberger
Copy link
Member

jberger commented May 27, 2015

Discussions (at length) on IRC have shown why it is significant to think of this case as distinct from the stringy ones, so on the issue of adding Mojo::Collection::TO_JSON I vote :+1:

kraih added a commit that referenced this pull request May 27, 2015
TO_JSON for Mojo::Collection
@kraih kraih merged commit 5ba810f into mojolicious:master May 27, 2015
@kraih
Copy link
Member

kraih commented May 27, 2015

Thanks, this pull request has been accepted by majority vote.

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

Successfully merging this pull request may close these issues.

None yet

7 participants