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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Mojo/Collection.pm
Expand Up @@ -9,6 +9,8 @@ use Scalar::Util 'blessed';

our @EXPORT_OK = ('c');

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

sub c { __PACKAGE__->new(@_) }

sub compact {
Expand Down Expand Up @@ -152,6 +154,12 @@ Construct a new array-based L<Mojo::Collection> object.

L<Mojo::Collection> implements the following methods.

=head2 TO_JSON

my $array = $collection->TO_JSON;

Alias for L</"to_array">.

=head2 compact

my $new = $collection->compact;
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/collection.t
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base -strict;
use Test::More;
use Mojo::ByteStream 'b';
use Mojo::Collection 'c';
use Mojo::JSON 'encode_json';

# Array
is c(1, 2, 3)->[1], 2, 'right result';
Expand Down Expand Up @@ -154,4 +155,9 @@ is_deeply $collection->uniq->to_array, [1, 2, 3, 4, 5], 'right result';
is_deeply $collection->uniq->reverse->uniq->to_array, [5, 4, 3, 2, 1],
'right result';

# json
$collection = c(1, 2, 3);
my $bytes = encode_json $collection;
is $bytes, '[1,2,3]', 'TO_JSON';

done_testing();