Skip to content

Commit

Permalink
Merge pull request #801 from wttw/master
Browse files Browse the repository at this point in the history
TO_JSON for Mojo::Collection
  • Loading branch information
kraih committed May 27, 2015
2 parents 1300205 + dc1ab3a commit 5ba810f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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();

0 comments on commit 5ba810f

Please sign in to comment.